综合开发

Why are the return values ​​of the form null in the MVC proje…

微信扫一扫,分享到朋友圈

Why are the return values ​​of the form null in the MVC proje…

I try to update a record .I use validation in this view but always

ModelState.IsValid = false

I search on the internet and found that I should check errors with this code :

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

Now I can not understand that what is problem? In this image you can see that all parameters are null.

Please advice. View:

@model TravelEnterAdminTemplate.Models.LG.ResturantModel
@{
ViewBag.Title = "EditRestaurant";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@*fck Editor*@
<script type="text/javascript" src="https://www.tuicool.com/articles/z6ZnEjz/@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="https://www.tuicool.com/articles/z6ZnEjz/@Url.Content(path + item.PhotoName)" alt="" class=" img-responsive" />
<img class="delimg" src="https://www.tuicool.com/articles/z6ZnEjz/~/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="https://www.tuicool.com/articles/z6ZnEjz/~/Content/Adminex/js/ajax/Restaurants_Edit.js"></script>
@section Scripts {
@Scripts.Render("~/bundles/jqueryValidation")
}

Model:

//------------------------------------------------------------------------------
// <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; }
}
}

I sometimes use Code like this for debugging. (written from memory)

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);
}
}
}

This should help you diagnose which model elements are causing validation errors.

A common reason for server side validation errors is that a non-nullable int or Guid Id field is included in the model but is either not included in the view, or is hidden and / or not populated.

Python fileinput 模块:命令行工具利器

上一篇

vue-axios设置公共的请求ip

下一篇

你也可能喜欢

评论已经被关闭。

插入图片

热门栏目

Why are the return values &ZeroWidthSpace;&ZeroWidthSpace;of the form null in the MVC proje…

长按储存图像,分享给朋友