我正在使用MVC5。我有一个模型调用User
,它有几个属性。其中一些是必需的,例如Password
和ConfirmPassword
。
看起来像这样。
public partial class Users
{
public long User_id { get; set; }
[Required]
public string Password { get; set; }
[NotMapped] // Does not effect with database
[Compare("Password")]
public virtual string ConfirmPassword { get; set; }
}
视图继承自模型,看起来像这样。
public class UserViewModel
{
public Users user { get; set; }
public IList<SelectListItem> AvailableCountries { get; set; }
}
当我更新用户时。我不会渲染密码和ConfirmPassoword。
我的更新Mehtod是这样的。
public async Task<ActionResult> Update(UserViewModel model)
{
ModelState.Remove("Password");
ModelState.Remove("ConfirmPassword");
if (ModelState.IsValid)
{
}
}
我想禁用验证,因此,正如在几个站点中所说的那样,我在控制器中添加以下内容
ModelState.Remove
但是当我要求ModelState.IsValid时,它总是假的......
这是因为数据验证位于Users
类,我是从UserViewModel
继承的吗?
为什么它不起作用?
我的观点在这里
@model TableAvivaVoz.Models.UserViewModel
@{
ViewBag.Title = "Edit";
}
<h2>Editar</h2>
@using (Html.BeginForm("Update", "Users", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Users</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(m => m.user.User_id)
<div class="form-group">
@Html.LabelFor(model => model.user.Country_id, "Country", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.user.Country_id, Model.AvailableCountries, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.user.Country_id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.user.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.user.Name, new { htmlAttributes = new { @class = "form-control", placeholder = "Nombre" } })
@Html.ValidationMessageFor(model => model.user.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.user.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.user.LastName, new { htmlAttributes = new { @class = "form-control", placeholder = "Apellido" } })
@Html.ValidationMessageFor(model => model.user.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.user.Sex, "Género", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Label("Hombre")
@Html.RadioButtonFor(model => model.user.Sex, "1", new { htmlAttributes = new { @class = "form-control" } })
@Html.Label("Mujer")
@Html.RadioButtonFor(model => model.user.Sex, "0", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.user.Sex, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.user.CodArea, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.user.CodArea, new { htmlAttributes = new { @class = "form-control", placeholder = "Cód Area" } })
@Html.ValidationMessageFor(model => model.user.CodArea, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.user.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.user.PhoneNumber, new { htmlAttributes = new { @class = "form-control", placeholder = "Número Telefono" } })
@Html.ValidationMessageFor(model => model.user.PhoneNumber, "", 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="col-md-10">
@Html.EditorFor(model => model.user.Email, new { htmlAttributes = new { @class = "form-control", placeholder = "Email" } })
@Html.ValidationMessageFor(model => model.user.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div style="position:relative;">
<label>Image</label>
<div>Upload new image: <input type="file" name="Image" /></div>
</div>
@if (Model.user == null || Model.user.Picture == null)
{
<div class="form-control-static">No Image</div>
}
else
{
<img class="img-thumbnail" width="100" height="100" src="data:image;base64,@System.Convert.ToBase64String(Model.user.Picture)" />
}
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
由于
答案 0 :(得分:0)
不要在您的实体中添加数据注释。您必须在ViewModel中添加数据注释,以便View是强类型的用户ViewModel。
模式 州考虑UserViwMode
因此,您必须在UserViewModel中删除public Users user { get; set; }
并添加此更改:
public class UserViewModel
{
puclic string Name { get; set; }
puclic string LastName { get; set; }
puclic bool sex { get; set; }
// and more ...
public IList<SelectListItem> AvailableCountries { get; set; }
}
答案 1 :(得分:0)
我解决了这个问题,就像斯蒂芬说的那样。我复制用户实体和UserModel并使用Mapper映射这些模型。就这样 example