MVC3下拉 - 在HttpPost模型状态错误 [英] MVC3 Dropdown - Model State Error on HttpPost
问题描述
我收到以下错误我DROPDOWNLIST的[HttpPost]方法。值被结合正确,这是没有问题的。但在模型状态所有的时间一个错误。该错误是:
从类型'System.String'的参数转换为键入'System.Web.Mvc.SelectListItem'失败,因为没有类型转换器可以将这些类型之间的转换。
在我的模型,我使用了以下内容。
公共类UploadDocumentViewModel { [显示(名称=文档标题)]
公共字符串DOCUMENTTITLE {搞定;组; } 公共IEnumerable的< SelectListItem>文件类型{搞定;组; } }
FileTypeViewModel:
公共类FileTypeViewModel
{
公共字符串FileTypeId {搞定;组; }
公共字符串FileTypeDescription {搞定;组; }
}
在控制器HTTPGET
[HTTPGET]
公众的ActionResult UploadDocument()
{ VAR文件类型= iFileTypeRepository.GetFileTypes(); //这是价值观的文件类型DROPDOWNLIST
UploadDocumentViewModel uploadDocumentViewModel =新UploadDocumentViewModel
{ FILETYPE = fileTypes.Select(X =>新建SelectListItem
{
文= x.FileTypeDescription,
值= Convert.ToString(x.FileTypeId)
})。了ToList()
};
返回查看(uploadDocumentViewModel); }
在[HttpPost]法
公众的ActionResult UploadDocument(的FormCollection形式,UploadDocumentViewModel uploadDocumentViewModel)
{
//文件类型
字符串ddlFileTypeSelectedValue = Convert.ToString(表格[文件类型]);
变种ddlFileType = iFileTypeRepository.GetFileTypes();
uploadDocumentViewModel.FileType =新的SelectList(ddlFileType,FileTypeId,FileTypeDescription,ddlFileTypeSelectedValue); //没有错误,然后提交
如果(ModelState.IsValid)
{
- 重定向到其他视图
} 其他
{
返回查看(uploadDocumentViewModel);
} }
在View
@model xxx.Core.Model.UploadDocumentViewModel
@ {
ViewBag.Title =上传文档;
}
< H2>
上传的客户端文件< / H>
@ Html.ValidationSummary()
@using(Html.BeginForm(UploadDocument,文档,FormMethod.Post))
{
< DIV>
<&字段集GT;
<传奇>上传的客户端文件< /传说>
< DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.DocumentTitle)
< / DIV>
< DIV CLASS =演示>
@ Html.TextBoxFor(型号=> model.DocumentTitle,新{@id =txtDocumentTitle})
@ Html.ValidationMessageFor(型号=> model.DocumentTitle)
< / DIV>
< DIV>
@ Html.LabelFor(型号=> model.FileType)
< / DIV>
< DIV>
@ Html.DropDownListFor(型号=> model.FileType,Model.FileType,请选择,新{@id =ddlFileType}) < / DIV>
< /字段集>
< / DIV>
< BR />
}
@ {Html.EndForm();}
更改此行
@ Html.DropDownListFor(型号=> model.FileType,Model.FileType,请选择,新{@id =ddlFileType})
到
@ Html.DropDownListFor(型号=> model.FileTypeId,Model.FileType,请选择,新{@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屋!