模型绑定在MVC 4中不起作用

时间:2014-04-22 12:28:15

标签: asp.net-mvc razor model-binding

我有一个表单,我发布到以下控制器操作方法。但货币值显示为空。为什么模型绑定没有发生?我已经在其他形式中使用了类似的方法,它曾经很好地工作。我不知道为什么它不起作用。

        [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; }
 }

2 个答案:

答案 0 :(得分:2)

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

public ActionResult EditCurrency_POST(CURRENCY currency)

public ActionResult EditCurrency_POST(CURRENCY myCurrencyModel)

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

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

答案 1 :(得分:0)

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

BeginForm(&#34;编辑&#34; ....

默认情况下,Razor会寻找

BeginForm(&#34; EditCurrency_POST&#34; ...)