私はMVC5で作業しています。私はいくつかのプロパティを持つモデルコールUser
を持っています。それらのうちのいくつか、例えばPassword
とConfirmPassword
が必要です。MVCデータ検証のみの挿入フォーム
このようになります。
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; }
}
ユーザーを更新するとき。私はパスワードとConfirmPasswordをレンダリングしません。
My Update Mehtodはこのようなものです。
public async Task<ActionResult> Update(UserViewModel model)
{
ModelState.Remove("Password");
ModelState.Remove("ConfirmPassword");
if (ModelState.IsValid)
{
}
}
私は無効に検証したかったので、それはseveralsサイトで言われているように、私は、コントローラ次
ModelState.Remove
しかし、私は尋ねに追加ModelState.IsValidの場合は常にfalse ...
これは、データ検証が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")
}
おかげ
あなたの質問はここにありますか? –
私はあなたの質問を理解していません –
私はちょうど今完成しました。ありがとうございました – Diego