0

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.'

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I changed submit button to input and put in <div class="form-group"></div> and problem solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.