我有一个模型属性绑定到HTML表单的文本框。但是,无论何时提交表单,该属性在到达控制器时始终为null。
之前我曾经无数次使用过相同的技术,但在这种情况下似乎并不想玩。
这是来自控制器的电话:
public ActionResult ScanTubsetLabelToClose()
{
CloseTubsetVM viewModel = new CloseTubsetVM();
return View(viewModel);
}
这是viewmodel:
public class CloseTubsetVM
{
public string ToClose { get; set; }
}
观点:
@using PR3Transfer.Models.ViewModels;
@model CloseTubsetVM
@using (Html.BeginForm("TubSetLabelToCloseScanned", "CloseTubset", new { closingBarcode = Model.ToClose, symbology = "KEY", }))
{
@Html.TextBoxFor(model => model.ToClose, new { @class = "scanTextbox", onkeypress = "posKeyPress(event)" })
<div>
<input class='hiddenButton' type="submit" value="Submit" />
</div>
}
这是目标控制器方法:
public ActionResult TubSetLabelToCloseScanned(string closingBarcode, string symbology)
{
// ...
}
我唯一的猜测是,当我在其他地方做同样的事情时,我正在逃避某些事情,但这又是什么?
答案 0 :(得分:0)
接受ToClose作为参数,您将获得模型属性的值
public ActionResult TubSetLabelToCloseScanned(string closingBarcode, string symbology, string ToClose)
{
// ...
}
答案 1 :(得分:0)
从我之前的评论:
属性名称需要与参数名称匹配。
所以,这是纠正的目标控制器方法:
public ActionResult TubSetLabelToCloseScanned(string barcode, string symbology)
{
//...
}
这是更正的观点:
@using PR3Transfer.Models.ViewModels;
@model CloseTubsetVM
@using (Html.BeginForm("TubSetLabelToCloseScanned", "CloseTubset", new { barcode = Model.Barcode, symbology = "KEY", }))
{
@Html.TextBoxFor(model => model.Barcode, new { @class = "scanTextbox", onkeypress = "posKeyPress(event)" })
<div>
<input class='hiddenButton' type="submit" value="Submit" />
</div>
}
答案 2 :(得分:0)
试试这个
public ActionResult TubSetLabelToCloseScanned(string ToClose, string symbology)
{
// ...
}