Frage

I'm trying to figure out a 'best practice' here and hope I'm not too far off. Here's the scenario:

Let's say we have two Actions. One for sending out a Razor View that contains a form. On this form, there is a <select> that is loaded with values from our ViewModel (let's say it's an expiration date field or something). There is also fields for the user to fill in, such as Name.

The other Action takes in the POST of the view, with the model as the parameter:

[HttpPost]
[ActionName("MyForm")]
public virtual ActionResult MyForm(MyViewModel model) {
    // logic here
}

Now, if there's some sort of issue with the model (let's say, we couldn't charge their credit card). We could do ModelState.AddModelError and then pass back the model:

return View(model);

However, is this a best practice? What about properties that are in our ViewModel, but we didn't "return back" from the page, such as our expiration date field values. These will now be NULL and we'll have an exception when loading the view.

Would best practice be to always "recreate" the view model before sending it back (somehow taking in the values they typed in, overlaying them onto the default viewmodel?) or should we have some method that always sets those "readonly" fields, and gets called in both Actions?

Hopefully this question makes sense. I don't know what to call the properties that are just "read only" on the view, and not passed back as part of the FORM POST.

Thanks.

War es hilfreich?

Lösung

You call read-only data, some call it system data, some buddy data (using enricher classes and Structuremap), often it is referred to as hydrating.

I usually approach it similar to this example ("a method which always sets those fields") or I'll create an action filter (OnActionExecuted) which injects buddy data depending on the type of the view model.

For example:

public class ContactFormData
{
    // data which gets posted back
    public string Name {get; set;}
    public string CountryCode {get; set;}

    // buddy data
    public SelectList Countries {get; set;}
}

[HttpGet]
[ActionName("ContactForm")]
public virtual ActionResult ContactForm() {
    var m = new ContactFormData();
    return ShowContactForm(m);
}

[HttpPost]
[ActionName("ContactForm")]
public virtual ActionResult ContactForm(ContactFormData formdata) {
    if (ModelState.IsValid)
    {
         // logic & redirect
         return redirect;
    }
    return ShowContactForm(formdata);
}

private ActionResult ShowContactForm(ContactFormData formdata)
{
    formData.Countries = GetCountriesSelectListSomewhere();
    return View(m);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top