我有一个模型,我创建并传递给我的视图。该模型创建了一个表单并使用该HttpPost
属性将模型取回。我将模型保存到数据库,然后我想将默认模型状态返回到视图(IE 下拉值,但不是所选项目)
我创建了一个空模型,将其传递给视图,但值保持不变,我不明白为什么。
我的观点
@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>
}
控制器:
[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;
做研究我发现如果我this.ModelState.Clear();
在创建新模型之前调用它,那么它可以工作,但为什么 MVC 会忽略我传递的模型并使用绑定的模型?