MVC 核心形式 Post Returns 空
MVC Core Form Post Returns Null
我的项目中的表单 post 操作有问题。这些是我的代码:
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>
但是当我 post 它的第一个输入值进入控制器但其他输入值变为空。当我点击提交按钮时,所有输入都有值,但我检查了所有内容,为什么会这样?
错误:
System.Data.SqlClient.SqlException: 'The parameterized query '(@siparisno int,@stokkodu nvarchar(5),@stokid int,@stokaciklamas' expects the parameter '@boy_y', which was not supplied.'
这是一个关于如何将 ViewModel 传递给操作的简单演示:
视图模型:
public class SiparisViewModel
{
public int? en_x { get; set; }
public int? boy_y { get; set; }
public int? adet_z { get; set; }
}
查看:
@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>
控制器:
[HttpPost]
public IActionResult Index(SiparisViewModel siparisViewModel)
{
return View();
}
结果:
我建议您检查视图或 js 文件中的名称是否正确,否则 not.This 可能导致值无法传递给操作。
我的项目中的表单 post 操作有问题。这些是我的代码:
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>
但是当我 post 它的第一个输入值进入控制器但其他输入值变为空。当我点击提交按钮时,所有输入都有值,但我检查了所有内容,为什么会这样?
错误:
System.Data.SqlClient.SqlException: 'The parameterized query '(@siparisno int,@stokkodu nvarchar(5),@stokid int,@stokaciklamas' expects the parameter '@boy_y', which was not supplied.'
这是一个关于如何将 ViewModel 传递给操作的简单演示:
视图模型:
public class SiparisViewModel
{
public int? en_x { get; set; }
public int? boy_y { get; set; }
public int? adet_z { get; set; }
}
查看:
@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>
控制器:
[HttpPost]
public IActionResult Index(SiparisViewModel siparisViewModel)
{
return View();
}
结果:
我建议您检查视图或 js 文件中的名称是否正确,否则 not.This 可能导致值无法传递给操作。