Viewmodel 值未返回到控制器
Viewmodel values not being returned to controller
我找到了一些与我的相似的 post,但找不到适合我需要的答案。
问题如下:
我有一个像这样的视图模型:
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>
}
在我的控制器中,我尝试按如下方式恢复 posted PrefViewModel:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(PrefViewModel TestMymodel)
{
if (ModelState.IsValid)
{
int myCountry = TestMymodel.mycountry;
int myprovince = TestMymodel.myprovince;
}
return View();
}
我的问题是 PrefViewModel TestMymodel 永远不会充满我认为我 post 返回的值。对我来说更奇怪的是,我确实取回了未经哈希处理的密码,但所有其他值都是 0 或 null。
我可以将值放在 PrefViewModel 中以加载页面并且可以正常工作,但在发布时它几乎完全是空的。
有什么想法吗?
编辑: 我将默认模型更改为我自己制作的模型会有什么不同吗?例如,当我调用 Create 操作时,我确实在我的 post 中取回了值(来自 create offcourse)。我有点绝望
edit2: 这就是 posted:
__RequestVerificationToken:-JYcw0CH2zZ7WrGUiYJM6-R6VxfL41ykTD5EHUjgtyyFcN01AaUU61BYuaRNr4oPdEvDq09aYsOFdb8fObJTXMnTKulADVkGY8CrBG3U71QXw0g7Th86WKl1up4059Zy7mW0SlrWGJpehed586v_5g2
user.Unhashed:乔纳斯1234-
user.Unhashed:乔纳斯1234-
ddlLand:1
ddlProvincie:3
(我的名声无法添加图片,所以这里一个link完整post:http://postimg.org/image/id95wjcxp/)
好的,当我将下拉列表的名称更改为 PrefViewModel 属性 名称时,这些值会正确返回。
您似乎已将下拉列表的名称覆盖为某些与视图模型中的 属性 名称不同的值。这就是为什么值没有成功绑定的原因。如果您希望默认模型绑定器能够将输入字段绑定回视图模型,请确保您的输入字段遵循与视图模型上的属性相同的名称。
您的用户名文本框也有禁用标志:
@Html.TextBoxFor(model => model.user.UserName, new { disabled = "disabled", @readonly = "readonly" })
所以不会提交回服务器。如果您希望这些值返回,您可能需要添加一个额外的隐藏字段。或者简单地使用 readonly
而不使用 disabled
属性。这两个属性都阻止用户修改相应输入字段中的值,但除此之外,disabled
属性在提交表单时将其从 POST 有效负载中剥离。
所以你可以使用:
@Html.TextBoxFor(model => model.user.UserName, new { @readonly = "readonly" })
我找到了一些与我的相似的 post,但找不到适合我需要的答案。 问题如下:
我有一个像这样的视图模型:
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>
}
在我的控制器中,我尝试按如下方式恢复 posted PrefViewModel:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(PrefViewModel TestMymodel)
{
if (ModelState.IsValid)
{
int myCountry = TestMymodel.mycountry;
int myprovince = TestMymodel.myprovince;
}
return View();
}
我的问题是 PrefViewModel TestMymodel 永远不会充满我认为我 post 返回的值。对我来说更奇怪的是,我确实取回了未经哈希处理的密码,但所有其他值都是 0 或 null。 我可以将值放在 PrefViewModel 中以加载页面并且可以正常工作,但在发布时它几乎完全是空的。
有什么想法吗?
编辑: 我将默认模型更改为我自己制作的模型会有什么不同吗?例如,当我调用 Create 操作时,我确实在我的 post 中取回了值(来自 create offcourse)。我有点绝望
edit2: 这就是 posted:
__RequestVerificationToken:-JYcw0CH2zZ7WrGUiYJM6-R6VxfL41ykTD5EHUjgtyyFcN01AaUU61BYuaRNr4oPdEvDq09aYsOFdb8fObJTXMnTKulADVkGY8CrBG3U71QXw0g7Th86WKl1up4059Zy7mW0SlrWGJpehed586v_5g2 user.Unhashed:乔纳斯1234- user.Unhashed:乔纳斯1234- ddlLand:1 ddlProvincie:3
(我的名声无法添加图片,所以这里一个link完整post:http://postimg.org/image/id95wjcxp/)
好的,当我将下拉列表的名称更改为 PrefViewModel 属性 名称时,这些值会正确返回。
您似乎已将下拉列表的名称覆盖为某些与视图模型中的 属性 名称不同的值。这就是为什么值没有成功绑定的原因。如果您希望默认模型绑定器能够将输入字段绑定回视图模型,请确保您的输入字段遵循与视图模型上的属性相同的名称。
您的用户名文本框也有禁用标志:
@Html.TextBoxFor(model => model.user.UserName, new { disabled = "disabled", @readonly = "readonly" })
所以不会提交回服务器。如果您希望这些值返回,您可能需要添加一个额外的隐藏字段。或者简单地使用 readonly
而不使用 disabled
属性。这两个属性都阻止用户修改相应输入字段中的值,但除此之外,disabled
属性在提交表单时将其从 POST 有效负载中剥离。
所以你可以使用:
@Html.TextBoxFor(model => model.user.UserName, new { @readonly = "readonly" })