C# 用MVC显示表单错误
我有一个表单可以上传图像文件并检查它们是否为JPG:C# 用MVC显示表单错误,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我有一个表单可以上传图像文件并检查它们是否为JPG: // CarAdmin/Index.cshtml @model MySite.Models.Car @using (Html.BeginForm("CarImageUpload", "Car", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="file" name="file" /> <input type="
// CarAdmin/Index.cshtml
@model MySite.Models.Car
@using (Html.BeginForm("CarImageUpload", "Car", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="text" name="imageInfo" />
<input type="submit" value="OK" />
}
<form action="CarAJAX" method="post" name="CarAdminForm">
<input name="Make" value="@Model.Name/>
<input type="submit" value="Update Car Info">
</form>
// CarController.cs
[HttpPost]
public ActionResult CarImageUpload(HttpPostedFileBase file)
{
ValidateImageFile V = new ValidateImageFile(file); // checks that the file is a jpg
List<String> Validity = V.Issues;
if (Validity.Count == 0)
{
file.SaveAs(V.FilePath);
}
else
{
Response.Write(String.Join("<br>", Validity.ToArray()); // THIS IS PROBLY WRONG
}
RedirectToAction("CarAdmin");
}
public ActionResult CarAdmin()
{
return View("CarAdmin/Index.cshtml");
}
//CarAdmin/Index.cshtml
@模型MySite.Models.Car
@使用(Html.BeginForm(“CarImageUpload”,“Car”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
}
用户Darian Dimitrov回答了一个与您的问题非常相似的问题,他的解决方案应该为您指明正确的方向
另一个很好的资源是:
您的视图可能如下所示:
// CarAdmin/Index.cshtml
@model MySite.Models.CarUploadViewModel
@using (Html.BeginForm("CarImageUpload", "Car", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="ImageUpload" />
<input type="text" name="ImageInfo" />
<input type="submit" value="OK" />
}
<form action="CarAJAX" method="post" name="CarAdminForm">
<input name="Make" value="@Model.Name/>
<input type="submit" value="Update Car Info">
</form>
public class CarUploadViewModel
{
[Required]
public string ImageInfo{ get; set; }
[DataType(DataType.Upload)]
HttpPostedFileBase ImageUpload { get; set; }
}
[HttpPost]
public ActionResult CarImageUpload(CarUploadViewModel model)
{
ValidateImageFile validity = new ValidateImageFile(model.ImageUpload); // checks that the file is a jpg
List<String> issues = validity.Issues;
if (issues.Count > 0)
{
// TODO: Add more descriptive issue messages
ModelState.AddModelError("ImageUpload", "There was an issue.");
}
if(ModelState.IsValid)
{
model.ImageUpload.SaveAs(V.FilePath);
RedirectToAction("CarAdmin");
}
return View(model);
}
您的控制器可能看起来像:
// CarAdmin/Index.cshtml
@model MySite.Models.CarUploadViewModel
@using (Html.BeginForm("CarImageUpload", "Car", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="ImageUpload" />
<input type="text" name="ImageInfo" />
<input type="submit" value="OK" />
}
<form action="CarAJAX" method="post" name="CarAdminForm">
<input name="Make" value="@Model.Name/>
<input type="submit" value="Update Car Info">
</form>
public class CarUploadViewModel
{
[Required]
public string ImageInfo{ get; set; }
[DataType(DataType.Upload)]
HttpPostedFileBase ImageUpload { get; set; }
}
[HttpPost]
public ActionResult CarImageUpload(CarUploadViewModel model)
{
ValidateImageFile validity = new ValidateImageFile(model.ImageUpload); // checks that the file is a jpg
List<String> issues = validity.Issues;
if (issues.Count > 0)
{
// TODO: Add more descriptive issue messages
ModelState.AddModelError("ImageUpload", "There was an issue.");
}
if(ModelState.IsValid)
{
model.ImageUpload.SaveAs(V.FilePath);
RedirectToAction("CarAdmin");
}
return View(model);
}
并将其输出到视图,如下所示:
@Html.ValidationMessage("MyField");
谢谢,你能举个例子吗?我对模型的外观有点困惑。我添加了另一个资源并更新了我的答案。请记住,我手边没有编译器,所以它可能不是100%,但这应该会有所帮助:)