Search code examples
modelasp.net-core-mvcform-post

MVC Core Form Post Returns Null


I have a problem with form post action on my project. These are my codes:

SiparisViewModel:

    public int? en_x { get; set; }
    public int? boy_y { get; set; }
    public int? adet_z { get; set; }

OlcuGirisi.cshtml

<div class="col-sm">
<input asp-for="en_x" class = "form-control" placeholder = "Genişlik" id = "Genislik" autocomplete = "off" BackColor = "#FFE3AA" onfocus = "onEnterGenislik()" onblur = "onLeaveGenislik()" onkeypress = "return isNumberKeyGenislik(event,this);"/>
</div>
<div class="col-sm">
<input asp-for="boy_y" class="form-control" placeholder="Yükseklik" id="Yukseklik" autocomplete="off" BackColor="#FFE3AA" onfocus="onEnterYukseklik()" onblur="onLeaveYukseklik()" onkeypress="return isNumberKeyYukseklik(event,this);" />
</div>
<div class="col-sm">
<input asp-for="adet_z" class="form-control" placeholder="Adet" id="Adet" autocomplete="off" BackColor="#FFE3AA" onfocus="onEnterAdet(); myFunction(this)" onblur="onLeaveAdet()" onkeypress="return isNumberKeyAdet(event,this);" />
</div>

But when I post it first input value goes to controller but other input values goes null. All inputs has values for sure when I click submit button but I checked everything, why could it be?

Error:

System.Data.SqlClient.SqlException: 'The parameterized query '(@siparisno int,@stokkodu nvarchar(5),@stokid int,@stokaciklamas' expects the parameter '@boy_y', which was not supplied.'


Solution

  • Here is a simple demo about how to pass ViewModel to the action:

    ViewModel:

    public class SiparisViewModel
        {
            public int? en_x { get; set; }
            public int? boy_y { get; set; }
            public int? adet_z { get; set; }
        }
    

    View:

     @model SiparisViewModel
        <form asp-action="Index">
            <div class="col-sm">
                <input asp-for="en_x" class="form-control" placeholder="Genişlik" id="Genislik" autocomplete="off" BackColor="#FFE3AA" onfocus="onEnterGenislik()" onblur="onLeaveGenislik()" onkeypress="return isNumberKeyGenislik(event,this);" />
            </div>
            <div class="col-sm">
                <input asp-for="boy_y" class="form-control" placeholder="Yükseklik" id="Yukseklik" autocomplete="off" BackColor="#FFE3AA" onfocus="onEnterYukseklik()" onblur="onLeaveYukseklik()" onkeypress="return isNumberKeyYukseklik(event,this);" />
            </div>
            <div class="col-sm">
                <input asp-for="adet_z" class="form-control" placeholder="Adet" id="Adet" autocomplete="off" BackColor="#FFE3AA" onfocus="onEnterAdet(); myFunction(this)" onblur="onLeaveAdet()" onkeypress="return isNumberKeyAdet(event,this);" />
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    

    Controller:

    [HttpPost]
            public IActionResult Index(SiparisViewModel siparisViewModel)
            {
                return View();
            }
    

    Result: enter image description here

    I suggest that you could check the name in the view or in the js file if it is right or not.This could cause the value can't pass to the action.