2017-01-10 5 views
0

私はMVC5で作業しています。私はいくつかのプロパティを持つモデルコールUserを持っています。それらのうちのいくつか、例えばPasswordConfirmPasswordが必要です。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") 
 
}

おかげ

+0

あなたの質問はここにありますか? –

+0

私はあなたの質問を理解していません –

+0

私はちょうど今完成しました。ありがとうございました – Diego

答えて

0

です。 ViewがユーザーのViewModelのタイプであるように、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; } 
} 
+0

OK Soheil ... UserViewModelがユーザーエンティティから継承する場合...UserプロパティからUserViewModelにデータ注釈を追加するにはどうすればよいですか?ありがとう – Diego

+0

データを投稿するビューコードを追加してください。 @Diego –

+0

私は自宅でそれを持っていない...私は明日を追加...ありがとう... – Diego

0

私は、問題を解決しました。 User EntityとUserModelを複製し、そのモデルをMapperでマップします。このように example

関連する問題