【问题标题】:Model Binding not working in MVC 4模型绑定在 MVC 4 中不起作用
【发布时间】: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


【解决方案1】:

尝试将控制器中的模型命名为货币以外的其他名称,即尝试更改:

public ActionResult EditCurrency_POST(CURRENCY currency)

public ActionResult EditCurrency_POST(CURRENCY myCurrencyModel)

然后显然改变了控制器中的其余内容。

我认为将传入变量命名为与您的类和此类的基础成员相同的名称可能会导致问题。

【讨论】:

  • 是的,这就是问题所在。我已经想通了,但无论如何感谢您的帮助。货币实际上是我的模型类中的字段名称,因此它不起作用
【解决方案2】:

由于您已将控制器方法从 EditCurrency_POST 更改为使用 C# 属性进行编辑,因此您必须在 HTML BeginForm 帮助程序中指定操作名称。

BeginForm("编辑" ....

默认情况下,Razor 会查找

BeginForm("EditCurrency_POST" ...)

【讨论】:

  • 不,它不工作,我仍然在控制器中得到空值。 @using (Html.BeginForm("Edit", "Currency" ,FormMethod.Post,null))
猜你喜欢
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
相关资源
最近更新 更多