C# 下拉列表始终将modelstate设置为无效
这有什么问题C# 下拉列表始终将modelstate设置为无效,c#,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,这有什么问题 @Html.DropDownListFor(x => x.Post.CategoryId, new SelectList(Model.Categories, "ID", "CategoryName"), "-- Please select --") @Html.ValidationMessageFor(x => x.Post.CategoryId) 当我提交表单时,modelstate无效,并且此下拉列表被标记为错误,即使我从下拉列表中选择了一个值 顺便说一句,Cat
@Html.DropDownListFor(x => x.Post.CategoryId, new SelectList(Model.Categories, "ID", "CategoryName"), "-- Please select --")
@Html.ValidationMessageFor(x => x.Post.CategoryId)
当我提交表单时,modelstate无效,并且此下拉列表被标记为错误,即使我从下拉列表中选择了一个值
顺便说一句,CategoryId是我的Post表中的FK
我已经添加了这个buddy类作为一个部分
[MetadataType(typeof(Post_Validation))]
public partial class Post
{
}
class Post_Validation
{
[Required(ErrorMessage = "Please select a Category")]
public int CategoryId { get; set; }
}
控制器:
// GET: /News/Add
[Authorize]
public ActionResult Add()
{
var model = new AddPostViewModel() { Categories = categoryRepository.GetAllCategories(), Post = new Post(), IsEditMode=false };
return View(model);
}
//
// POST: /News/Add
[Authorize]
[HttpPost]
public ActionResult Add(AddPostViewModel model)
{
if (!ModelState.IsValid)
{
..etc
}
}
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new AddPostViewModel()
{
Categories = Enumerable.Range(1, 5).Select(x => new SelectListItem
{
Value = x.ToString(),
Text = "category " + x
}),
Post = new Post()
};
return View(model);
}
[HttpPost]
public ActionResult Index(AddPostViewModel model)
{
if (!ModelState.IsValid)
{
...
}
...
}
}
您发布的这个特定代码没有问题。可能错误的是您在发布到的控制器操作中所期望的模型。例如,如果此模型与用于渲染此视图的模型不同:
[HttpPost]
public ActionResult Index(Post post) { ... }
而不是:
[HttpPost]
public ActionResult Index(ModelContainingPostConmtainingCategoryId post) { ... }
生成的dropdownlist的名称如下:name=Post.CategoryId,如果控制器操作不采用具有名为Post的属性和名为CategoryId的属性的模型,并且此CategoryId属性标记为Required,则您将获得所描述的行为
其他可能的原因是,您有额外的属性,这些属性是必需的,但尚未包含在您的请求中。找到这一点的方法是分析控制器操作中返回的模型,并查看缺少的内容。在调试模式下查看ModelState属性本身也会告诉您哪些属性存在验证错误
更新:
下面是一个完整的工作示例,可以用作基础:
型号:
[MetadataType(typeof(Post_Validation))]
public class Post
{
public int CategoryId { get; set; }
}
class Post_Validation
{
[Required(ErrorMessage = "Please select a Category")]
public int CategoryId { get; set; }
}
public class AddPostViewModel
{
public IEnumerable<SelectListItem> Categories { get; set; }
public Post Post { get; set; }
}
视图:
如果您选择的变量类型不是字符串,则您的模型状态在下拉列表中始终无效。模型显示良好,当我删除类别DDL上的validationfor时,它提交正常。CategoryId是一个int…这可能是一个问题吗?@raklos,如果你在下拉列表中使用非整数作为值,它可能是一个问题。而且它只会发生在可为空的整数上。如果您使用的是不可为空的整数,则在这种情况下会出现异常。CategoryId是否在您发布到的控制器操作中分配了一个值?CategoryId返回为null。。ID都是int,在select中查看每个选项的源值是一个整数too@raklos,如果CategoryId定义为int,则不能为null。整数是值类型,值类型不能为null。它们有一个默认值,对于整数,该默认值为0。因此,问题正如我所怀疑的:您的绑定工作不正常:下拉列表中的选定值未正确绑定到控制器操作中的值。当然,如果不显示您的代码,我无法进一步帮助您,因为我不知道您的模型看起来如何,也不知道您发布到的控制器操作。@raklos,我可以看到您对模型的更新。您忘了发布控制器操作的样子。
@model AppName.Models.AddPostViewModel
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm())
{
@Html.DropDownListFor(x => x.Post.CategoryId, new SelectList(Model.Categories, "Value", "Text"), "-- Please select --")
@Html.ValidationMessageFor(x => x.Post.CategoryId)
<input type="submit" value="OK" />
}