HttpGet和HttpPost返回验证错误并加载SelectList

时间:2014-09-26 14:34:38

标签: c# asp.net-mvc

我有一个基于ViewModel的表单,它提示用户将Person的XApplication状态更改为Accepted或Rejected。

当我通过POST提交表单时,我想创建一个新的XApplication然后如果成功则将用户带回同一页面。如果它包含错误,那么我希望通过传递View(personApp)再次重新加载表单,这将通过剃刀方法@Html.ValidationSummary()显示错误。

但是,当我尝试执行此操作时,View()不了解我的SelectLists,并且无法使用" Application Accepted"填充下拉列表。和#34;申请被拒绝"。

我可以采取另一种方法吗?

[Route("student/xApplication/{personId?}")]
[HttpGet]
public ActionResult xApplication(int? personId)
{
    if (personId == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Person person = db.People.Find(personId);
    if (person == null)
    {
        return HttpNotFound();
    }

    // Prepare ViewModel to pass to view, based on personId
    PersonViewModel personVModel = new PersonViewModel();
    personVModel.Person = person;

    // Get XApplication Data
    var xApps = from a in db.XApplications where a.personId == personId select a;

    XApplication personXApplication = null;
    if (xApps.Count() > 0)
    {
        personXApplication = xApps.First();
    }
    personVModel.XApplications = personXApplication;

    List<SelectListItem> applicationStatusItems = new List<SelectListItem>();
    applicationStatusItems.Add(new SelectListItem { Value = "1", Text = "Application Accepted" });
    applicationStatusItems.Add(new SelectListItem { Value = "0", Text = "Application Rejected" });
    SelectList applicationStatusList = new SelectList(applicationStatusItems, "Value", "Text", null);

    personVModel.ApplicationStatusList = applicationStatusList;

    return View(personVModel);
}

[HttpPost]
[Route("student/xApplication")]
public ActionResult xApplication(PersonViewModel personVModel)
{
    if (ModelState.IsValid)// Checks no errors
    {
        db.XApplications.Add(personVModel.XApplications);
        db.SaveChanges();
        return Redirect("/student/xApplication/" + personVModel.Person.id);
    }

    return View(personVModel);
}

1 个答案:

答案 0 :(得分:1)

由于SelectList是硬编码的,我建议在PersonViewModel内生成它们,以避免代码重复。您可以通过将生成SelectList的代码移动到名为PopulateApplicationStatusList的公共方法并在构造函数中调用它来实现此目的

public class PersonViewModel
{
    public PersonViewModel()
    {
        this.PopulateApplicationStatusList();
    }

    public SelectList ApplicationStatusList { get; set; }

    public void PopulateApplicationStatusList()
    {
        List<SelectListItem> applicationStatusItems = new List<SelectListItem>();
        applicationStatusItems.Add(new SelectListItem { Value = "1", Text = "Application Accepted" });
        applicationStatusItems.Add(new SelectListItem { Value = "0", Text = "Application Rejected" });
        this.ApplicationStatusList = new SelectList(applicationStatusItems, "Value", "Text", null);
    }
}

在您的获取操作方法中,当您创建ApplicationStatusList

的新实例时,将生成PersonViewModel
[Route("student/xApplication/{personId?}")]
[HttpGet]
public ActionResult xApplication(int? personId)
{
    if (personId == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Person person = db.People.Find(personId);
    if (person == null)
    {
        return HttpNotFound();
    }

    // Prepare ViewModel to pass to view, based on personId
    PersonViewModel personVModel = new PersonViewModel(); // ApplicationStatusList is generated here
    personVModel.Person = person;

    // Get XApplication Data
    var xApps = from a in db.XApplications where a.personId == personId select a;

    XApplication personXApplication = null;
    if (xApps.Count() > 0)
    {
        personXApplication = xApps.First();
    }
    personVModel.XApplications = personXApplication;

    return View(personVModel);
}

在您的帖子操作方法中,您可以通过调用ApplicationStatusList方法

在出现任何错误时重新填充PopulateApplicationStatusList属性
[HttpPost]
[Route("student/xApplication")]
public ActionResult xApplication(PersonViewModel personVModel)
{
    if (ModelState.IsValid)// Checks no errors
    {
        db.XApplications.Add(personVModel.XApplications);
        db.SaveChanges();
        return Redirect("/student/xApplication/" + personVModel.Person.id);
    }

    // repopulate ApplicationStatusList property
    personVModel.PopulateApplicationStatusList();

    return View(personVModel);
}