简体   繁体   English

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

[英]HttpGet and HttpPost return validation errors and loading SelectList

I have a form which is based on a ViewModel that prompts the user to change a Person's XApplication status to either Accepted or Rejected. 我有一个基于ViewModel的表单,该表单提示用户将Person的XApplication状态更改为Accepted或Rejected。

When I submit the form via POST I want to Create a new XApplication and then if successful take the user back to the same page. 当我通过POST提交表单时,我想创建一个新的XApplication ,然后如果成功,则将用户带回到同一页面。 If it contains errors then I wish to reload the form again, by passing View(personApp) , which will display the errors via the razor method @Html.ValidationSummary() . 如果它包含错误,那么我希望通过传递View(personApp)再次重新加载表单,该View(personApp)将通过剃刀方法@Html.ValidationSummary()显示错误。

However when I try and do this the View() doesn't know about my SelectLists and can't populate the dropdown with "Application Accepted" and "Application Rejected". 但是,当我尝试执行此操作时,View()不了解我的SelectList,因此无法使用“接受的应用程序”和“拒绝的应用程序”填充下拉列表。

Is there another approach I can take? 我可以采取另一种方法吗?

[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);
}

Since the SelectList is hard coded, I would suggest generating them inside PersonViewModel to avoid code duplication. 由于SelectList是硬编码的,因此建议您在PersonViewModel内部生成它们,以避免代码重复。 You can do so by moving the code that generates the SelectList to a public method named PopulateApplicationStatusList and call it in the constructor 您可以通过将生成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);
    }
}

In your get action method, ApplicationStatusList will be generated when you create a new instance of PersonViewModel 在您的GET操作方法, 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);
}

In your post action method, you can repopulate ApplicationStatusList property when there's any errors by calling PopulateApplicationStatusList method 在后操作方法中,如果发生任何错误,可以通过调用PopulateApplicationStatusList方法来重新填充ApplicationStatusList属性

[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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM