2016-03-27 7 views
-1

私はインターネット上で検索し、私はこのコードでエラーをチェックする必要がありますことを発見.Iは、このビューで検証を使用するが、常になぜフォームのreturened値がMVCプロジェクトでnullですか?

ModelState.IsValid = falseを

レコードを更新してみてください:

var errors = ModelState.Values.SelectMany(v => v.Errors); 

enter image description here 今私は何が問題であることを理解できないのですか? この画像では、すべてのパラメータがnullであることがわかります。 enter image description here

お願いします。 ビュー:

@model TravelEnterAdminTemplate.Models.LG.ResturantModel 
@{ 
    ViewBag.Title = "EditRestaurant"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
@*fck Editor*@ 
<script type="text/javascript" src="@Url.Content("~/Content/Adminex/js/fckEditor/fckeditor.js")"></script> 
<script type="text/javascript"> 
    window.onload = function() { 
     var sBasePath = '@Url.Content("~/Content/Adminex/js/fckeditor/")'; 
     var oFCKeditor = new FCKeditor('Restaurants_Description'); 
     oFCKeditor.BasePath = sBasePath; 
     oFCKeditor.Height = 450; 
     oFCKeditor.Width = 800; 
     oFCKeditor.ReplaceTextarea(); 
     //----------------------DescriptionEn--------------------- 
     var oFCKeditorEn = new FCKeditor('Restaurants_DescriptionEn'); 
     oFCKeditorEn.BasePath = sBasePath; 
     oFCKeditorEn.Height = 450; 
     oFCKeditorEn.Width = 800; 
     oFCKeditorEn.ReplaceTextarea(); 
    } 
</script> 
<div class="container"> 
    <h4>ویرایش رستوران</h4> 
    <hr /> 

    @using (Html.BeginForm()) 
    { 
     @Html.AntiForgeryToken() 

     <div class="form-horizontal"> 


      @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 


      @Html.HiddenFor(model => model.Restaurants.id) 



      <div class="row"> 
       <div class="col-md-4 pull-right"> 
        @Html.LabelFor(model => model.Restaurants.RestaurantName, htmlAttributes: new { @lablefor = "RestaurantName" }) 


        @Html.EditorFor(model => model.Restaurants.RestaurantName, new { htmlAttributes = new { @class = "form-control" } }) 

       </div> 
       <div class="col-md-4 pull-right">@Html.ValidationMessageFor(model => model.Restaurants.RestaurantName, "", new { @class = "text-danger" })</div> 
      </div> 

      <div class="row"> 
       <div class="col-md-7 pull-right"> 
        @Html.LabelFor(model => model.Restaurants.Address, htmlAttributes: new { @lablefor = "Address" }) 


        @Html.EditorFor(model => model.Restaurants.Address, new { htmlAttributes = new { @class = "form-control" } }) 

       </div> 
       <div class="col-md-4 pull-right">@Html.ValidationMessageFor(model => model.Restaurants.Address, "", new { @class = "text-danger" })</div> 
      </div> 

      <div class="row"> 
       <div class="col-md-2 pull-right"> 
        @Html.LabelFor(model => model.Restaurants.City.Country.NameFa, htmlAttributes: new { @lablefor = "CountryId" }) 
        @Html.DropDownList("CountryId", null, new { @class = "form-control" }) 
       </div> 
      </div> 

      <div class="row"> 
       <div class="col-md-2 pull-right"> 
        <div> 
         @Html.LabelFor(model => model.Restaurants.City.NameFA, htmlAttributes: new { @lablefor = "CityId" }) 
         @Html.DropDownList("CityId", null, new { @class = "form-control" }) 
        </div> 
       </div> 
      </div> 

      <div class="row"> 
       <div class="col-md-2 pull-right"> 
        @Html.LabelFor(model => model.Restaurants.Tell, htmlAttributes: new { @lablefor = "Tell" }) 
        @Html.EditorFor(model => model.Restaurants.Tell, new { htmlAttributes = new { @class = "form-control" } }) 

       </div> 
       <div class="col-md-4 pull-right">@Html.ValidationMessageFor(model => model.Restaurants.Tell, "", new { @class = "text-danger" })</div> 
      </div> 

      <div class="row"> 
       <div class="col-md-4 pull-right"> 
        @Html.LabelFor(model => model.Restaurants.Title, htmlAttributes: new { @lablefor = "Title" }) 


        @Html.EditorFor(model => model.Restaurants.Title, new { htmlAttributes = new { @class = "form-control" } }) 

       </div> 
       <div class="col-md-4 pull-right"> 
        @Html.ValidationMessageFor(model => model.Restaurants.Title, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

      <div class="row"> 
       <div class="col-md-3 pull-right"> 
        @Html.LabelFor(model => model.Restaurants.InsertDate, htmlAttributes: new { @lablefor = "InsertDate" }) 


        @Html.EditorFor(model => model.Restaurants.InsertDate, new { htmlAttributes = new { @class = "form-control" } }) 

       </div> 
       <div class="col-md-4 pull-right">@Html.ValidationMessageFor(model => model.Restaurants.InsertDate, "", new { @class = "text-danger" })</div> 
      </div> 

      <div class="row"> 
       <div class="col-md-5 pull-right"> 
        @Html.LabelFor(model => model.Restaurants.Summary, htmlAttributes: new { @lablefor = "Summary" }) 


        @Html.EditorFor(model => model.Restaurants.Summary, new { htmlAttributes = new { @class = "form-control" } }) 

       </div> 
       <div class="col-md-4 pull-right"> 
        @Html.ValidationMessageFor(model => model.Restaurants.Summary, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

      <div class="row"> 
       <div class="col-md-8 pull-right"> 
        @Html.LabelFor(model => model.Restaurants.Description, htmlAttributes: new { @lablefor = "Description" }) 


        @Html.EditorFor(model => model.Restaurants.Description, new { htmlAttributes = new { @class = "form-control" } }) 

       </div> 
       <div class="col-md-4 pull-right">@Html.ValidationMessageFor(model => model.Restaurants.Description, "", new { @class = "text-danger" })</div> 
      </div> 

      <div class="row"> 
       <div class="col-md-3 pull-right"> 
        @Html.LabelFor(model => model.Restaurants.LocationX, htmlAttributes: new { @lablefor = "LocationX" }) 


        @Html.EditorFor(model => model.Restaurants.LocationX, new { htmlAttributes = new { @class = "form-control" } }) 

       </div> 
       <div class="col-md-4 pull-right"> 
        @Html.ValidationMessageFor(model => model.Restaurants.LocationX, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

      <div class="row"> 
       <div class="col-md-3 pull-right"> 
        @Html.LabelFor(model => model.Restaurants.LocationY, htmlAttributes: new { @lablefor = "LocationY" }) 


        @Html.EditorFor(model => model.Restaurants.LocationY, new { htmlAttributes = new { @class = "form-control" } }) 

       </div> 
       <div class="col-md-4 pull-right">@Html.ValidationMessageFor(model => model.Restaurants.LocationY, "", new { @class = "text-danger" })</div> 
      </div> 

      <div class="row"> 
       <div class="col-md-3 pull-right"> 
        @Html.LabelFor(model => model.Restaurants.Website, htmlAttributes: new { @lablefor = "Website" }) 


        @Html.EditorFor(model => model.Restaurants.Website, new { htmlAttributes = new { @class = "form-control website" } }) 

       </div> 
       <div class="col-md-4 pull-right">@Html.ValidationMessageFor(model => model.Restaurants.Website, "", new { @class = "text-danger" })</div> 
      </div> 
      <div class="row"> 

       <div class="col-md-5 pull-right"> 
        <div class="checkbox"> 
         @Html.LabelFor(model => model.Restaurants.IsActive, htmlAttributes: new { @lablefor = "IsActive" }) 
         @Html.EditorFor(model => model.Restaurants.IsActive) 

        </div> 
        <div class="col-md-4 pull-right"> 
         @Html.ValidationMessageFor(model => model.Restaurants.IsActive, "", new { @class = "text-danger" }) 
        </div> 
       </div> 
      </div> 
      @*-------------------------------------------------------------------------------------*@ 
      <fieldset dir="ltr" class="fieldsetEn"> 
       <legend>English Content</legend> 
       <div class="row"> 
        <div class="col-md-5"> 
         <div class="checkbox checkboxEn"> 
          @Html.LabelFor(model => model.Restaurants.IsEnglish, htmlAttributes: new { @Labelfor = "IsEnglish" }) 
          @Html.EditorFor(model => model.Restaurants.IsEnglish) 
         </div> 
         <div class="col-md-4 "> 
          @Html.ValidationMessageFor(model => model.Restaurants.IsEnglish, "", new { @class = "text-danger" }) 
         </div> 
        </div> 
       </div> 

       <div class="row"> 
        <div class="col-md-4"> 
         @Html.LabelFor(model => model.Restaurants.RestaurantNameEn, htmlAttributes: new { @Labelfor = "RestaurantNameEn" }) 
         @Html.EditorFor(model => model.Restaurants.RestaurantNameEn, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 
        <div class="col-md-4 "> 
         @Html.ValidationMessageFor(model => model.Restaurants.RestaurantNameEn, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

       <div class="row"> 
        <div class="col-md-7"> 
         @Html.LabelFor(model => model.Restaurants.AddressEn, htmlAttributes: new { @Labelfor = "AddressEn" }) 
         @Html.EditorFor(model => model.Restaurants.AddressEn, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 
        <div class="col-md-4 "> 
         @Html.ValidationMessageFor(model => model.Restaurants.AddressEn, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

       <div class="row"> 
        <div class="col-md-4"> 
         @Html.LabelFor(model => model.Restaurants.TitleEn, htmlAttributes: new { @Labelfor = "TitleEn" }) 
         @Html.EditorFor(model => model.Restaurants.TitleEn, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 
        <div class="col-md-4 "> 
         @Html.ValidationMessageFor(model => model.Restaurants.TitleEn, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

       <div class="row"> 
        <div class="col-md-5"> 
         @Html.LabelFor(model => model.Restaurants.SummaryEn, htmlAttributes: new { @Labelfor = "" }) 
         @Html.EditorFor(model => model.Restaurants.SummaryEn, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 
        <div class="col-md-4 "> 
         @Html.ValidationMessageFor(model => model.Restaurants.SummaryEn, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

       <div class="row"> 
        <div class="col-md-7"> 
         @Html.LabelFor(model => model.Restaurants.DescriptionEn, htmlAttributes: new { @Labelfor = "DescriptionEn" }) 
         @Html.TextAreaFor(model => model.Restaurants.DescriptionEn, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 
        <div class="col-md-4 "> 
         @Html.ValidationMessageFor(model => model.Restaurants.DescriptionEn, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

      </fieldset> 

      @{ 
     string path = System.Configuration.ConfigurationManager.AppSettings["ImageEdit"]; 
      } 
      <div class="row text-right"> 
       @foreach (var item in Model.PhotoTables) 
       { 

        <text> 
         <div class="col-xs-2 col-wrapper pull-right"> 
          <div class="image-wrapper"> 
           <img src="@Url.Content(path + item.PhotoName)" alt="" class=" img-responsive" /> 
           <img class="delimg" src="~/Content/Adminex/images/delete-icons.png" id="@item.Id" alt="حذف تصویر" /> 
          </div> 
         </div> 
        </text> 

       } 
      </div> 

      <br /> 
      <br /> 

      <div class="row"> 
       <div class="col-md-4"></div> 
       <div class="col-md-4"> 
        <input type="submit" value="ذخیره تغییرات" class="btn btn-info btn-block" /> 
       </div> 
       <div class="col-md-4"></div> 
       </div> 
     </div> 
    } 
</div> 
<script src="~/Content/Adminex/js/ajax/Restaurants_Edit.js"></script> 
@section Scripts { 
    @Scripts.Render("~/bundles/jqueryValidation") 
} 

モデル:

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated from a template. 
// 
//  Manual changes to this file may cause unexpected behavior in your application. 
//  Manual changes to this file will be overwritten if the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace TravelEnterAdminTemplate.Models.dbModel 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class Restaurant 
    { 
     public int id { get; set; } 
     public int CityId { get; set; } 
     public string RestaurantName { get; set; } 
     public string Address { get; set; } 
     public string Tell { get; set; } 
     public string Title { get; set; } 
     public System.DateTime InsertDate { get; set; } 
     public string Summary { get; set; } 
     public string Description { get; set; } 
     public string LocationX { get; set; } 
     public string LocationY { get; set; } 
     public string UserId { get; set; } 
     public int Visitor { get; set; } 
     public bool IsActive { get; set; } 
     public string Website { get; set; } 
     public int CategoryId { get; set; } 
     public string RestaurantNameEn { get; set; } 
     public string AddressEn { get; set; } 
     public string TitleEn { get; set; } 
     public string SummaryEn { get; set; } 
     public string DescriptionEn { get; set; } 
     public Nullable<int> VisitorEn { get; set; } 
     public bool IsEnglish { get; set; } 

     public virtual AspNetUser AspNetUser { get; set; } 
     public virtual City City { get; set; } 
    } 
} 
+0

あなたのビュー内のモデルが 'ResturantModel'が、中モデルであり、あなたのビューとのviewmodelコード –

+1

を投稿してくださいあなたのPOSTメソッドは 'レストラン'です(彼らは同じである必要があります!) –

答えて

1

私は時々、デバッグのために、このようなコードを使用します。

var errors = new List<KeyValuePair<string, string>(); 
for (int i = 0; i < ModelState.Keys.Count; i++) 
{ 
    var value = ModelState.Values.ElementAt(i); 
    if value.Errors.Any() 
    { 
      foreach (var error in Value.Errors) 
      { 
       errors.Add(new KeyValuePair<string, string> 
       (ModelState.Keys.ElementAt(i), 
        error.Message ?? error.Exception.Message); 
      } 
     } 
} 

これは、検証エラーの原因となっているモデル要素を診断するのに役立ちます。

サーバー側の検証エラーの一般的な理由は、nullを許可しないintまたはGuid Idフィールドがモデルに含まれていますが、ビューに含まれていないか、表示されていないか、または入力されていないことです。

+0

あなたは正しいです。私はこのコードを使用し、そのエラーを発見した。ErrorMessageは、検証のためのMete Dataの私のマッサージであり、error.Exception.Messageはnullです。つまり、フォームの戻り値はすべてnullです。今これを修正する方法は? – programmer138200

+0

ポイントは、無効なフィールドを識別し、問題を修正することです。ビューとモデルを素早く見て、それがあなたのCategoryIdであることを示唆しています。 –

0

あなたが投稿する方法ではありません。

あなたのポストの方法は、何か非常に単純である必要があります。タスク・ポスト(ModelClass someNameという)非同期

公共

ので、あなたは基本的にモデルクラスを持っています。そのクラスはSystem.Runtime.Serializationへの参照を必要とする[DataContract]で装飾する必要があります

投稿の一部として送信できるようにするそのクラスの各プロパティは、DataMemberタグをオンにする必要がありますそれ。このようにして、MVCエンジンで使用されるデフォルトのシリアライゼーションは、フォームで渡された名前がモデル内の名前と正確に一致する限り、ポストを受け入れて受け入れます。これは特にWebApiで作業する場合に当てはまります。

(ModelState.IsValid!)場合は、エラーコード

にあなたがあなたのモデルプロパティに持っているすべての検証項目を返す:純粋なMVCの仕事のために我々は

は、必ず最初のモデルの検証を行い、これらのタグは必要ありません。 ModelStateが有効になる前に発生する必要があります。ここで

は、私が数年前に書いた基本的な例ですが、多分それは役立ちます:

mvc article

関連する問題