视图模型值未返回到控制器
本文关键字:控制器 返回 模型 视图 | 更新日期: 2023-09-27 18:37:02
我找到了一些与我类似的帖子,但我找不到适合我需求的答案。问题如下:
我有一个这样的视图模型:
public class PrefViewModel
{
public SelectList countries { get; set; }
public SelectList Provincies { get; set; }
public ApplicationUser user { get; set; }
public Preference MyPref{ get; set; }
public int mycountry { get; set; }
public int myprovince { get; set; }
}
我的 cshtml 看起来像这样:
@using (Html.BeginForm("Index","Preferences", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(model => model.user.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="control-label col-md-10">
<span class="textvak">
@Html.TextBoxFor(model => model.user.UserName, new { disabled = "disabled", @readonly = "readonly" })
</span>
@Html.ValidationMessageFor(model => model.user.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.user.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="control-label col-md-10">
<span class="textvak">
@Html.TextBoxFor(model => model.user.Email, new { disabled = "disabled", @readonly = "readonly" })
</span>
@Html.ValidationMessageFor(model => model.user.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.user.Unhashed, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.user.Unhashed, new { htmlAttributes = new { @class = "form-control", type = "password" } })
@Html.ValidationMessageFor(model => model.user.Unhashed, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.user.Provincie.Land, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="control-label col-md-10">
@Html.DropDownListFor(model => model.mycountry, Model.countries, new { Name = "ddlLand", id = "ddlLanden", @class = "textvak" })
@Html.ValidationMessageFor(model => model.user.Provincie.Land, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.user.Provincie, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="control-label col-md-10">
@Html.DropDownListFor(model => model.myprovince, Model.Provincies, new { @class = "textvak" })
@Html.ValidationMessageFor(model => model.user.Provincie, "", new { @class = "text-danger" })
</div>
</div>
<br />
<table>
<tr>
<td>
<input type="submit" value=@Resources.Wijzig class="btn btn-default" />
</td>
</tr>
</table>
}
在我的控制器中,我尝试按如下方式恢复发布的PrefViewModel:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(PrefViewModel TestMymodel)
{
if (ModelState.IsValid)
{
int myCountry = TestMymodel.mycountry;
int myprovince = TestMymodel.myprovince;
}
return View();
}
我的问题是PrefViewModel TestMymodel从来没有填充过我认为我正在回发的值。对我来说更奇怪的是,我确实得到了未散列的密码,但所有其他值都是 0 或 null。我可以将值放入 PrefViewModel 中以加载页面并且有效,但在发布时它几乎完全是空的。
有什么想法吗?
编辑:我确实将默认模型更改为我自己制作的模型会有什么不同吗?例如,当我调用"创建"操作时,我确实在我的帖子中(从创建 offcourse)中恢复了值。我有点绝望
编辑2:这是发布的内容:
__RequestVerificationToken:-JYcw0CH2zZ7WrGUiYJM6-R6VxfL41ykTD5EHUjgtyyFcN01AaUU61BYuaRNr4oPdEvDq09aYsOFdb8fObJTXMnTKulADVkGY8CrBG3U71QXw0g7Th86WKl1up4059Zy7mW0SlrWGJpehed586v_5g2用户。未散列:乔纳斯1234-用户。未散列:乔纳斯1234-日土地:1DDL省:3
(不能添加带有我的声誉的图片,所以这里有一个完整帖子的链接:http://postimg.org/image/id95wjcxp/)
好的,当我将下拉列表的名称更改为 PrefViewModel 属性名称时,这些值将返回正确。
您似乎已将下拉列表的名称覆盖为某些与视图模型中的属性名称不同的值。这就是值未成功绑定回的原因。如果您希望默认模型绑定程序能够将它们绑定回视图模型,请确保输入字段遵循与视图模型的属性相同的名称。
此外,您的用户名文本框具有禁用标志:
@Html.TextBoxFor(model => model.user.UserName, new { disabled = "disabled", @readonly = "readonly" })
因此,它不会提交回服务器。如果您希望这些值返回,则可能需要添加其他隐藏字段。或者干脆使用没有disabled
属性的readonly
。这两个属性都阻止用户修改相应输入字段中的值,但除此之外,disabled
属性会在提交表单时将其从 POST 有效负载中删除。
因此,您可以使用:
@Html.TextBoxFor(model => model.user.UserName, new { @readonly = "readonly" })