Asp.net mvc 有人能解释一下MVC模型绑定吗?

Asp.net mvc 有人能解释一下MVC模型绑定吗?,asp.net-mvc,model-binding,Asp.net Mvc,Model Binding,我有一个模型,我创建并传递给我的视图。模型创建了一个表单,并使用HttpPost属性获取模型。我将模型保存到数据库,然后我想将默认模型状态返回到视图(即下拉值,但不是所选项目) 我创建了一个空模型,将其传递给视图,但值保持不变,我不明白为什么 我的看法 @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend> <h2&g

我有一个模型,我创建并传递给我的视图。模型创建了一个表单,并使用
HttpPost
属性获取模型。我将模型保存到数据库,然后我想将默认模型状态返回到视图(即下拉值,但不是所选项目)

我创建了一个空模型,将其传递给视图,但值保持不变,我不明白为什么

我的看法

 @using (Html.BeginForm())
  {
    @Html.ValidationSummary(true)
    <fieldset>
      <legend>
        <h2>Configuration settings</h2>
      </legend>

      <div class="editor-label">
        @Html.LabelFor(model => model.DeviceType)
      </div>
      <div class="editor-field">
        @Html.DropDownListFor(model => model.DeviceTypeSelectedItem, new SelectList(Model.DeviceType, "Value", "Text"),new {@class = "DeviceTypeDDL"})
        @Html.ValidationMessageFor(model => model.DeviceTypeSelectedItem)
      </div>

       <div class="editor-label">
        @Html.LabelFor(model => model.ConfigGroup)
      </div>
      <div class="editor-field">
        @Html.DropDownListFor(model => model.ConfigGroupSelectedItem, new SelectList(Model.ConfigGroup, "Value", "Text"),new {@class = "ConfigGroupDDL"})
        @Html.ValidationMessageFor(model => model.ConfigGroupSelectedItem)
      </div>

       <div class="editor-label">
        @Html.LabelFor(model => model.ConfigName)
      </div>
      <div class="editor-field">
        @Html.DropDownListFor(model => model.ConfigNameSelectedItem, new SelectList(Model.ConfigName, "Value", "Text"),new {@class = "ConfigNameDDL"})
        @Html.ValidationMessageFor(model => model.ConfigNameSelectedItem)
      </div>

        <div class="editor-label">
        @Html.LabelFor(model => model.ConfigValue)
      </div>
      <div class="editor-field">
        @Html.EditorFor(model => model.ConfigValue)
        @Html.ValidationMessageFor(model => model.ConfigValue)
      </div>

        <div class="editor-label">
        @Html.LabelFor(model => model.MergeOrDelete)
      </div>
      <div class="editor-field">
        @Html.DropDownListFor(model => model.MergeOrDeleteSelectedItem, new SelectList(Model.MergeOrDelete, "Value", "Text"), new { @class = "MergeDeleteDDL" })
        @Html.ValidationMessageFor(model => model.MergeOrDeleteSelectedItem)
      </div>

      @Html.HiddenFor(model => model.ManagementGroupId)

      <p>
        <input type="submit" value="Add" />
      </p>
    </fieldset>
  }
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
配置设置
@LabelFor(model=>model.DeviceType)
@DropDownListFor(model=>model.DeviceTypeSelectedItem,new SelectList(model.DeviceType,“Value”,“Text”),new{@class=“DeviceTypeDDL”})
@Html.ValidationMessageFor(model=>model.DeviceTypeSelectedItem)
@LabelFor(model=>model.ConfigGroup)
@DropDownListFor(model=>model.ConfigGroupSelectedItem,new SelectList(model.ConfigGroup,“Value”,“Text”),new{@class=“ConfigGroupDDL”})
@Html.ValidationMessageFor(model=>model.ConfigGroupSelectedItem)
@LabelFor(model=>model.ConfigName)
@DropDownListFor(model=>model.ConfigNameSelectedItem,新建SelectList(model.ConfigName,“Value”,“Text”),新建{@class=“confignamedll”})
@Html.ValidationMessageFor(model=>model.ConfigNameSelectedItem)
@LabelFor(model=>model.ConfigValue)
@EditorFor(model=>model.ConfigValue)
@Html.ValidationMessageFor(model=>model.ConfigValue)
@LabelFor(model=>model.MergeOrDelete)
@DropDownListFor(model=>model.MergeOrDeleteSelectedItem,new SelectList(model.MergeOrDelete,“Value”,“Text”),new{@class=“mergedeletedll”})
@Html.ValidationMessageFor(model=>model.MergeOrDeleteSelectedItem)
@Html.HiddenFor(model=>model.ManagementGroupId)

}
控制器:

[HttpGet]
    public ActionResult Index(int id)
    {
      var model = CreateDefaultConfigModel(id);
      return View(model);
    }
    [HttpPost]
    public ActionResult Index(vmConfiguration model)
    {
      if (ModelState.IsValid)
      {
        bool isMerge = model.MergeOrDeleteSelectedItem == 1 ? true : false;
        _configLogic.AddConfigurationValue((int)model.ConfigNameSelectedItem, (int)model.ManagementGroupId, model.ConfigValue, isMerge);
        return View(CreateDefaultConfigModel(model.ManagementGroupId));
      }
      else
      {
        return View(model);
      }
    }
private vmConfiguration CreateDefaultConfigModel(int id)
{
  var model = new vmConfiguration
  {
    DeviceType = _configLogic.GetDevices,
    ConfigGroup = new List<SelectListItem>() { EmptySelect() },
    ConfigName = new List<SelectListItem>() { EmptySelect() },
    ConfigGroupSelectedItem = null,
    MergeOrDeleteSelectedItem = null,
    DeviceTypeSelectedItem = null,
    ConfigNameSelectedItem = null,
    ManagementGroupId = id,
    ParamData = _configLogic.GetParamValuesForGroup(id)
  };
  return model;
}
private static SelectListItem EmptySelect()
{
  return new SelectListItem { Text = "No value", Value = "-1" };
}
private ConfigurationLogic _configLogic;
[HttpGet]
公共行动结果索引(int id)
{
var模型=CreateDefaultConfigModel(id);
返回视图(模型);
}
[HttpPost]
公共行动结果索引(vmConfiguration模型)
{
if(ModelState.IsValid)
{
bool isMerge=model.MergeOrDeleteSelectedItem==1?真:假;
_configLogic.AddConfigurationValue((int)model.ConfigNameSelectedItem,(int)model.ManagementGroupId,model.ConfigValue,isMerge);
返回视图(CreateDefaultConfigModel(model.ManagementGroupId));
}
其他的
{
返回视图(模型);
}
}
专用vmConfiguration CreateDefaultConfigModel(int id)
{
var模型=新的vmConfiguration
{
DeviceType=\u configLogic.GetDevices,
ConfigGroup=new List(){EmptySelect()},
ConfigName=新列表(){EmptySelect()},
ConfigGroupSelectedItem=null,
MergeOrDeleteSelectedItem=null,
DeviceTypeSelectedItem=null,
ConfigNameSelectedItem=null,
ManagementGroupId=id,
ParamData=\u configLogic.GetParamValuesForGroup(id)
};
收益模型;
}
私有静态SelectListItem EmptySelect()
{
返回新的SelectListItem{Text=“No value”,value=“-1”};
}
专用配置逻辑\u配置逻辑;
通过研究,我发现如果调用
this.ModelState.Clear()在创建新模型之前,它就可以工作了,但是为什么MVC会忽略我传递的模型而使用绑定的模型呢?

return View(CreateDefaultConfigModel(model.ManagementGroupId));
对此

return RedirectToAction("Index", new { id = model.ManagementGroupId });

它并没有忽略您的模型,而是在您将表单发送回同一索引页时尝试填写表单,因为它假定您将表单发送回是因为需要更多信息,并且它试图通过不强制用户再次输入所有信息来提供帮助。这些信息都存储在ModelState中,因为它保存了发回服务器的尝试值。无论如何,在成功发布后重定向是推荐的模式。查找

模型是否有效,值是否按预期更新?嗨,chris,是的,一切都有效,一切正常,没有错误。问题是我没有预料到会发生什么,我想知道为什么。我觉得这很好,你确定else语句没有通过调试器运行吗?因为我认为这就是正在发生的事情。您提到“无错误”,但ModelState错误不会作为异常引发。没有模型错误。我传递了一个空模型,但它仍然绑定到上一个模型。谢谢Yarx,这就是我一直在寻找的答案。