【发布时间】:2014-04-23 00:12:30
【问题描述】:
我有一个要发布到以下控制器操作方法的表单。但货币价值显示为空。为什么模型绑定没有发生?我在其他形式中使用过类似的方法,它曾经工作得很好。我不知道为什么它不起作用。
[ActionName("Edit")]
[HttpPost]
public ActionResult EditCurrency_POST(CURRENCY currency) //currency = null;
{
DAL lib = new DAL();
int state = lib.UpdateCurrency(currency);
if (state == 1)
{
return RedirectToAction("Details", new { id = currency.ID });
}
else
{
return View("Error");
}
}
//查看我发帖的来源:
@model Library.CURRENCY
@{
ViewBag.Title = "EditCurrency_GET";
}
<h2>EditCurrency_GET</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>VFS_CURRENCY</legend>
@Html.HiddenFor(model => model.ID)
<div class="editor-label">
@Html.LabelFor(model => model.CURRENCY)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CURRENCY)
@Html.ValidationMessageFor(model => model.CURRENCY)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CURRENCY_SYMBOL)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CURRENCY_SYMBOL)
@Html.ValidationMessageFor(model => model.CURRENCY_SYMBOL)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CURRENCY_CODE)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CURRENCY_CODE)
@Html.ValidationMessageFor(model => model.CURRENCY_CODE)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ISAVTIVE)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ISAVTIVE)
@Html.ValidationMessageFor(model => model.ISAVTIVE)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DESCRIPTION)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DESCRIPTION)
@Html.ValidationMessageFor(model => model.DESCRIPTION)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
//我的模型是
public partial class CURRENCY
{
public CURRENCY()
{
this.COUNTRY = new HashSet<COUNTRY>();
}
public int ID { get; set; }
public string CURRENCY { get; set; }
public string CURRENCY_SYMBOL { get; set; }
public int CURRENCY_CODE { get; set; }
public bool ISAVTIVE { get; set; }
public string DESCRIPTION { get; set; }
public virtual ICollection<COUNTRY> COUNTRY { get; set; }
}
【问题讨论】:
-
如果我检查 chrome 中的值,我可以看到这些值被传递,如果我设置断点,我会看到在 action 方法中分配了 null
-
发布您的模型,我常见的是模型使用字段而不是属性。模型绑定仅适用于属性。
-
您需要在
@using (Html.BeginForm()) {中添加动作名称 -
试试这个
EditCurrency_POST(CURRENCY model)改变你的模型对象名称。 -
我已经添加了我的模型类
标签: asp.net-mvc razor model-binding