- 首页
- 其他开发
- MVC3 下拉列表 - HttpPost 上的模型状态错误
MVC3 下拉列表 - HttpPost 上的模型状态错误
[英] MVC3 Dropdown - Model State Error on HttpPost
本文介绍了MVC3 下拉列表 - HttpPost 上的模型状态错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 [HttpPost] 方法的下拉列表出现以下错误.值绑定正确,这不是问题.但是模型状态一直存在错误.错误是:
从类型System.String"到类型System.Web.Mvc.SelectListItem"的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换.
在我的模型中,我使用了以下内容.
公共类 UploadDocumentViewModel {[显示(名称=文档标题")]公共字符串文档标题 { 获取;放;}公共 IEnumerable文件类型 { 获取;放;}}
文件类型视图模型:
公共类 FileTypeViewModel{公共字符串 FileTypeId { 获取;放;}公共字符串 FileTypeDescription { 获取;放;}}
在控制器 HttpGet 中
[HttpGet]公共 ActionResult UploadDocument(){var fileTypes = iFileTypeRepository.GetFileTypes();//这是用于 FileType DropDownlist 的值UploadDocumentViewModel uploadDocumentViewModel = 新的 UploadDocumentViewModel{FileType = fileTypes.Select(x => new SelectListItem{文本 = x.FileTypeDescription,值 = Convert.ToString(x.FileTypeId)}).ToList()};返回视图(上传文档视图模型);}
在[HttpPost]方法中
public ActionResult UploadDocument(FormCollection form,UploadDocumentViewModel uploadDocumentViewModel){//文件类型字符串 ddlFileTypeSelectedValue = Convert.ToString(form["FileType"]);var ddlFileType = iFileTypeRepository.GetFileTypes();uploadDocumentViewModel.FileType = new SelectList(ddlFileType, "FileTypeId", "FileTypeDescription", ddlFileTypeSelectedValue);//没有错误,然后提交如果(模型状态.IsValid){-- 重定向到其他一些视图}别的{返回视图(上传文档视图模型);}}
在视图中
@model xxx.Core.Model.UploadDocumentViewModel@{ViewBag.Title = "上传文件";}<h2>上传客户文档@Html.ValidationSummary()@using (Html.BeginForm("UploadDocument", "Document", "FormMethod.Post")){<div><字段集><legend>上传客户文档</legend><div class="editor-label">@Html.LabelFor(model => model.DocumentTitle)
<div class="demo">@Html.TextBoxFor(model => model.DocumentTitle, new { @id = "txtDocumentTitle" })@Html.ValidationMessageFor(model =>model.DocumentTitle)
<div>@Html.LabelFor(model => model.FileType)
<div>@Html.DropDownListFor(model => model.FileType, Model.FileType, "请选择", new { @id = "ddlFileType" })
</fieldset>
<br/>}@{Html.EndForm();}
解决方案
更改此行
@Html.DropDownListFor(model => model.FileType, Model.FileType, "请选择", new { @id = "ddlFileType" })
到
@Html.DropDownListFor(model => model.FileTypeId, Model.FileType, "请选择", new { @id = "ddlFileType" })
并在您的模型中添加一个字符串属性 FileTypeId
,以便在您发布时获得所选项目
I am getting the following error for my Dropdownlist on [HttpPost] Method. The values are binding correctly, that is not a problem. But there is an error in model state all the time. The error is:
The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed because no type converter can convert between these types.
In my model I am using the following.
public class UploadDocumentViewModel {
[Display(Name = "Document Title")]
public string DocumentTitle { get; set; }
public IEnumerable<SelectListItem> FileType { get; set; }
}
FileTypeViewModel:
public class FileTypeViewModel
{
public string FileTypeId { get; set; }
public string FileTypeDescription { get; set; }
}
In the Controller HttpGet
[HttpGet]
public ActionResult UploadDocument()
{
var fileTypes = iFileTypeRepository.GetFileTypes(); // This is for FileType DropDownlist of values
UploadDocumentViewModel uploadDocumentViewModel = new UploadDocumentViewModel
{
FileType = fileTypes.Select(x => new SelectListItem
{
Text = x.FileTypeDescription,
Value = Convert.ToString(x.FileTypeId)
}).ToList()
};
return View(uploadDocumentViewModel);
}
In [HttpPost] Method
public ActionResult UploadDocument(FormCollection form,UploadDocumentViewModel uploadDocumentViewModel )
{
//FileTypes
string ddlFileTypeSelectedValue = Convert.ToString(form["FileType"]);
var ddlFileType = iFileTypeRepository.GetFileTypes();
uploadDocumentViewModel.FileType = new SelectList(ddlFileType, "FileTypeId", "FileTypeDescription", ddlFileTypeSelectedValue);
// No Errors, then Submit
if (ModelState.IsValid)
{
-- Redirect to some other View
}
else
{
return View(uploadDocumentViewModel);
}
}
In View
@model xxx.Core.Model.UploadDocumentViewModel
@{
ViewBag.Title = "Upload Document";
}
<h2>
Upload Client Document</h2>
@Html.ValidationSummary()
@using (Html.BeginForm("UploadDocument", "Document", "FormMethod.Post"))
{
<div>
<fieldset>
<legend>Upload Client Document</legend>
<div class="editor-label">
@Html.LabelFor(model => model.DocumentTitle)
</div>
<div class="demo">
@Html.TextBoxFor(model => model.DocumentTitle, new { @id = "txtDocumentTitle" })
@Html.ValidationMessageFor(model => model.DocumentTitle)
</div>
<div>
@Html.LabelFor(model => model.FileType)
</div>
<div>
@Html.DropDownListFor(model => model.FileType, Model.FileType, "Please Select", new { @id = "ddlFileType" })
</div>
</fieldset>
</div>
<br />
}
@{Html.EndForm();}
解决方案
Change this line
@Html.DropDownListFor(model => model.FileType, Model.FileType, "Please Select", new { @id = "ddlFileType" })
to
@Html.DropDownListFor(model => model.FileTypeId, Model.FileType, "Please Select", new { @id = "ddlFileType" })
And add a string property FileTypeId
to your model, so that when you post you get the selected item
这篇关于MVC3 下拉列表 - HttpPost 上的模型状态错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!