C# Viewmodel值未返回到控制器
我找到了一些和我类似的帖子,但是我找不到一个适合我需要的答案。 问题如下: 我有这样一个viewmodel:C# Viewmodel值未返回到控制器,c#,asp.net,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc 5,我找到了一些和我类似的帖子,但是我找不到一个适合我需要的答案。 问题如下: 我有这样一个viewmodel: public class PrefViewModel { public SelectList countries { get; set; } public SelectList Provincies { get; set; } public ApplicationUser user { get; set; } public Preference MyPre
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 TestMymodel中从来没有填充过我想发回的值。更让我感到奇怪的是,我确实获得了未删除的密码,但所有其他值都是0或null。
我可以在PrefViewModel中输入值来加载页面,这是可行的,但在发布时它几乎完全是空的
有什么想法吗
编辑:我将默认模型更改为我自己制作的模型会有什么不同吗?因为当我调用Create操作时,我确实从Create offcourse中获取了帖子中的值。我有点绝望了
编辑2:这是发布的内容:
__请求验证依据:-JYcw0CH2zZ7WrGUiYJM6-R6VXFL41YKTD5EHUJGTYFCN01AAUUU61由UARNR4OPDEVDQ09AYSOFDB8FOBJTXMNTKULADVKGY8CRBG3U71QXW0G7TH86 WKL14059ZY7MW0SLRWGJPE586Vā5g2
user.Unhashed:Jonas1234-
user.Unhashed:Jonas1234-
土地:1
省:3
无法添加具有我的声誉的图片,因此这里有一个指向完整帖子的链接:
好的,当我将dropdownlists的名称更改为PrefViewModel属性名称时,返回的值是正确的 您似乎已将下拉列表的名称覆盖为与视图模型中的特性名称不同的某些值。这就是为什么这些值没有成功绑定回来。如果希望默认模型绑定器能够将输入字段绑定回视图模型,请确保输入字段的名称与视图模型上的属性相同 您的用户名文本框还具有禁用标志:
@Html.TextBoxFor(model => model.user.UserName, new { disabled = "disabled", @readonly = "readonly" })
因此,它不会被提交回服务器。如果希望这些值返回,可能需要添加额外的隐藏字段。或者只使用readonly而不使用disabled属性。这两个属性都阻止用户修改相应输入字段中的值,但除此之外,禁用的属性在提交表单时将其从POST有效负载中删除
因此,您可以使用:
@Html.TextBoxFor(model => model.user.UserName, new { @readonly = "readonly" })
你能找回mycountry和myprovince的值吗?不,我没有。我只得到了用户。在我的TestMyModel中,你能显示发布的内容吗?从客户端发送的实际有效负载。您可以使用web浏览器的“开发人员”工具栏的“网络”选项卡来捕获这些内容。我在Edit2Wrude下的原始问题中添加了发布的内容,有一些ddlLand和ddlLand值被发送到服务器,而这些值在您的标记中不存在。您是否使用javascript提交此表单?或者一些js插件增强了这些下拉列表并对其进行了重命名?这就解释了为什么只有你得到的只有未删除的值-这是唯一提交给服务器的东西。非常感谢。你救了我一天!
@Html.TextBoxFor(model => model.user.UserName, new { @readonly = "readonly" })