Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在我从控制器向下拉列表发送模型数据的视图页面中显示验证错误?_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 如何在我从控制器向下拉列表发送模型数据的视图页面中显示验证错误?

C# 如何在我从控制器向下拉列表发送模型数据的视图页面中显示验证错误?,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有一个国家模型(Id,名称),类型(Id,地点类型)模型和地点(细节)模型每当我想创建一个新地方时,我必须发送一个包含国家和类型列表的viewModel我必须从查看页面的下拉列表中选择国家和类型。我正在使用数据注释来验证数据。问题是,当输入表单无效时,我必须显示红色的空字段错误,并且必须保留ViewModel数据。如果重定向到创建页面,则无法显示错误消息;如果使用返回视图(“创建”),则ViewModel变为空 我该怎么做 视图模型: public class DropDownListView

我有一个国家模型(Id,名称),类型(Id,地点类型)模型和地点(细节)模型
每当我想创建一个新地方时,我必须发送一个包含国家和类型列表的viewModel
我必须从查看页面的下拉列表中选择国家和类型。我正在使用数据注释来验证数据。问题是,当输入表单无效时,我必须显示红色的空字段错误,并且必须保留ViewModel数据。如果重定向到创建页面,则无法显示错误消息;如果使用返回视图(“创建”),则ViewModel变为空
我该怎么做
视图模型:

public class DropDownListViewModel
{
    [Required(ErrorMessage = "Address is required")]
    public PlaceModel Place { get; set; }
    public IEnumerable<TypeModel> TypeList { get; set; }
    public IEnumerable<CountryModel> CountryList { get; set; }
    [Required]
    public int SelectedType { get; set;  }
    public int SelectedCountry { get; set; }
}

public class PlaceModel:BaseClassModel
{
    [Required(ErrorMessage ="Address is required")]
    public string Address { get; set; }
    [Range(1, 5, ErrorMessage = "Range must be between 1-5 in years.")]
    public int RatingId { get; set; }
    public string TypeName { get; set; }
    public string CountryName { get; set; }

    public byte[] img { get; set; }
}

[HttpPost]
    public ActionResult SaveNewPlace(DropDownListViewModel EditedModel)
    {   
        if(ModelState.IsValid)
        {

            DBhandler db = new DBhandler();

            string Name = EditedModel.Place.Name;
            string Address = EditedModel.Place.Address;
            int RatingId = EditedModel.Place.RatingId;
            int TypeId = EditedModel.SelectedType;
            int CountryId = EditedModel.SelectedCountry;
            if (EditedModel.Place.Id >= 1)
            {
                int Id = EditedModel.Place.Id;
                db.Update(Id, Name, Address, RatingId, TypeId, CountryId);
            }
            else
            {
                db.Insert(Name, Address, RatingId, TypeId, CountryId);
            }
            return Redirect("Index");
        }
        else
        {
            return View("Create");
        }
    }

public ActionResult Create()
    {
        DBhandler manager = new DBhandler();
        DropDownListViewModel list = new DropDownListViewModel();
        list.CountryList = manager.GetCountries();
        list.TypeList = manager.GetTypes();
        return View(list);
    }
public类DropDownListViewModel
{
[必需(ErrorMessage=“Address is Required”)]
公共场所模型场所{get;set;}
公共IEnumerable类型列表{get;set;}
公共IEnumerable CountryList{get;set;}
[必需]
public int SelectedType{get;set;}
public int SelectedCountry{get;set;}
}
公共类PlaceModel:BaseClassModel
{
[必需(ErrorMessage=“Address is Required”)]
公共字符串地址{get;set;}
[范围(1,5,ErrorMessage=“范围必须在1-5年之间。”)]
公共整数比率ID{get;set;}
公共字符串类型名{get;set;}
公共字符串CountryName{get;set;}
公共字节[]img{get;set;}
}
[HttpPost]
公共操作结果SaveNewPlace(DropDownListViewModel EditedModel)
{   
if(ModelState.IsValid)
{
DBhandler db=新的DBhandler();
字符串名称=EditedModel.Place.Name;
字符串地址=EditedModel.Place.Address;
int RatingId=EditedModel.Place.RatingId;
int TypeId=EditedModel.SelectedType;
int CountryId=EditedModel.SelectedCountry;
如果(EditedModel.Place.Id>=1)
{
int Id=EditedModel.Place.Id;
数据库更新(Id、名称、地址、等级Id、类型Id、国家Id);
}
其他的
{
db.插入(名称、地址、等级ID、类型ID、国家ID);
}
返回重定向(“索引”);
}
其他的
{
返回视图(“创建”);
}
}
公共操作结果创建()
{
DBhandler管理器=新的DBhandler();
DropDownListViewModel列表=新的DropDownListViewModel();
list.CountryList=manager.GetCountries();
list.TypeList=manager.GetTypes();
返回视图(列表);
}
视图:

@model TouristPlace.Models.DropDownListViewModel
@{
ViewBag.Title=“查看”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
看法
@使用(Html.BeginForm(“SaveNewPlace”、“Home”、FormMethod.Post))
{
@Html.AntiForgeryToken()
PlaceModel

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.Place.Name,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Place.Name,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Place.Name,“,new{@class=“text danger”}) @LabelFor(model=>model.Place.Address,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Place.Address,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Place.Address,“,new{@class=“text danger”}) @LabelFor(model=>model.Place.RatingId,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Place.RatingId,new{htmlAttributes=new{@class=“form control”,min=“1”,max=“5”,value=“1”}}) @Html.ValidationMessageFor(model=>model.Place.RatingId,“,new{@class=“text danger”}) @LabelFor(model=>model.SelectedType,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“SelectedType”,new SelectList(Model.TypeList,“Id”,“Name”),new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.SelectedType,“,new{@class=“text danger”}) @LabelFor(model=>model.SelectedCountry,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“SelectedCountry”,new SelectList(Model.CountryList,“Id”,“Name”),new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.SelectedCountry,“,new{@class=“text danger”}) } @ActionLink(“返回列表”、“索引”)
在不指定返回页的情况下尝试,以便:

else
    {
        return View();
    }

不重定向,将viewmodel发送回视图返回视图(“索引”,EditedModel);它实际上不起作用。给我这个错误
传入字典的模型项的类型为“TouristPlace.Models.DropDownListViewModel”,但此字典需要类型为“System.Collections.Generic.List`1[TouristPlace.Models.PlaceModel]”的模型项。这是一种非常常见的模式。在控制器中的私有方法中填充下拉列表,如果ModelState无效,则从GET和POST调用它。例子。我们使用第三方下拉列表(Kendo),它可以进行自己的AJAX调用来填充下拉列表。太好了:)那个搜索视图页面不是和SaveNewPlace同名吗?我的理解是,如果您指定这样的返回页面:返回视图(“索引”);它将重新加载页面,包含错误的模型将消失。您希望将相同的模型返回到页面或返回到视图。请记住,您的模型包含错误,您必须