Asp.net mvc 模型绑定在MVC4中不起作用

Asp.net mvc 模型绑定在MVC4中不起作用,asp.net-mvc,razor,model-binding,Asp.net Mvc,Razor,Model Binding,我有一个表单,我正在发布到以下控制器操作方法。但货币值显示为空。为什么模型绑定没有发生?我在其他形式中也使用过类似的方法,而且效果很好。我不知道为什么它不起作用 [ActionName("Edit")] [HttpPost] public ActionResult EditCurrency_POST(CURRENCY currency) //currency = null; { DAL lib = new D

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

        [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>
}
@model Library.CURRENCY
@{
ViewBag.Title=“EditCurrency\u-GET”;
}
编辑货币
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
货币
@Html.HiddenFor(model=>model.ID)
@LabelFor(model=>model.CURRENCY)
@EditorFor(model=>model.CURRENCY)
@Html.ValidationMessageFor(model=>model.CURRENCY)
@LabelFor(model=>model.CURRENCY\u符号)
@EditorFor(model=>model.CURRENCY\u符号)
@Html.ValidationMessageFor(model=>model.CURRENCY\u符号)
@LabelFor(model=>model.CURRENCY\u代码)
@EditorFor(model=>model.CURRENCY\u代码)
@Html.ValidationMessageFor(model=>model.CURRENCY\u代码)
@LabelFor(model=>model.ISAVTIVE)
@EditorFor(model=>model.ISAVTIVE)
@Html.ValidationMessageFor(model=>model.ISAVTIVE)
@LabelFor(model=>model.DESCRIPTION)
@EditorFor(model=>model.DESCRIPTION)
@Html.ValidationMessageFor(model=>model.DESCRIPTION)

}
//我的模型是

  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; }
 }
公共部分类货币
{
公共货币()
{
this.COUNTRY=new HashSet();
}
公共int ID{get;set;}
公共字符串货币{get;set;}
公共字符串货币_符号{get;set;}
公共整数货币_代码{get;set;}
公共bool是vtive{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection国家{get;set;}
}

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

BeginForm(“编辑”

默认情况下,Razor将查找


BeginForm(“EditCurrency\u POST”…)

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

public ActionResult EditCurrency_POST(CURRENCY currency)

然后,可以明显地更改控制器中的其余内容


我认为,将传入变量命名为与您的类和该类的底层成员相同可能会导致问题。

如果我检查chrome中的值,我可以看到传递的值,如果我设置了断点,我可以看到在操作方法中分配了null。发布您的模型,我的共同点是模型使用字段而不是属性。Model binding仅对属性有效。您需要使用(Html.BeginForm()){在
@中添加操作名称,使用此
EditCurrency\u POST(货币模型)
更改您的模型对象名称。我已添加了模型类否它不起作用,并且我在控制器中仍然为null。@使用(Html.BeginForm)(“编辑”,“货币”,FormMethod.Post,null)是的,这就是问题所在。我已经解决了这个问题,但无论如何感谢您的帮助。currency实际上是我的模型类中字段的名称,所以它不起作用
public ActionResult EditCurrency_POST(CURRENCY myCurrencyModel)