0

I have a top10 table (like top 10 restaurants say). Each top 10 row can have up to 10 top10items associated.

Models

public class Top10
{
    public Guid Id { get; set; }

    [Required(ErrorMessage="Title Required")]
    public string Title { get; set; }
}

public class Top10Item
{
    public Guid Id { get; set; }
    public Guid Top10Id { get; set; }
    public Guid PlaceId { get; set; }
    public Int16 Position { get; set; }

    public Place place { get; set; }
}

View Model

public class Top10ItemsPlaceDropdown
{
    public Top10 top10 { get; set; }

    [Display(Name = "Place")]
    public Guid SelectedPlaceId { get; set; }

    public IEnumerable<Place> Places { get; set; }

    public IEnumerable<SelectListItem> PlaceItems
    {
        get
        {
            return new SelectList(Places, "Id", "PlaceName");
        }
    }

    public IEnumerable<Top10Item> items { get; set; }
}

I bind the ViewModel to a View.

I use it to populate a dropdown on the page, and populate a grid of items for the top 10. The user can then select an item from the dropdown - which is then posted by the [httpPost] method when the user submits the form.

The problem - if it is a problem - is that when the view loads [httpGet] - all properties - and those of all sub-objects are valid. However when the view posts back to the controller [httpPost], many are empty (invalid) because on the view they aren't in form fields and don't get posted back (they don't need to be).

This means Model.IsValid == false. If I had a validation summary on the page - this would invoke it.

This doesn't seem like good practice - but what should i do about this? The model serves my purposes when the page loads - but all I need when the page is posted is top10Id and SelectedPlaceId.

7
  • 1
    This question is self-answering: if your model state is not valid, but it doesn't need to be, then it doesn't matter by definition. Commented Sep 21, 2016 at 22:30
  • What does the controller return from this POST ? The same view, a partial one, some JSON, ... ?
    – Philippe
    Commented Sep 22, 2016 at 6:06
  • It feels like bad practice - and for example it is triggering a validationsummary - this is just one example of why I think it's a bad idea? The post returns the same object passed to the view - (which is normal no?) I didn't realise you could "return" a partial view to a controller - I thought partials were essentially just "includes"?).
    – niico
    Commented Sep 22, 2016 at 15:06
  • If your application sends a full view to the client with an invalid view model, isn't the resulting screen broken ?
    – Philippe
    Commented Sep 22, 2016 at 19:04
  • what do you mean by broken? Yes there is a validationsummary which is triggered though I could remove it. What do you suggest?
    – niico
    Commented Sep 23, 2016 at 0:33

1 Answer 1

0

The first GET returns a View built from a valid ViewModel.

The POST then returns a View built from an invalid ViewModel, giving validation warnings.

If the second View is the same as the first, the ViewModel must be valid, or the display won't be correct, because of the missing properties.

If the second View is different from the first, you should use another ViewModel, which contains only the properties needed for the second View, without validation warnings

5
  • How can you use a different view model between get and post?
    – niico
    Commented Sep 24, 2016 at 22:10
  • 2 ways: 1) the action method in your controller has a parameter with the second ViewModel type. As long as it has a property SelectedPlaceId, MVC will fill it. 2) the action method in your controller has a Guid parameter named SelectedPlaceId, MVC will fill it from your request, You then create an instance of the second ViewModel with the correct SelectidPlaceId.
    – Philippe
    Commented Sep 25, 2016 at 13:41
  • By the way, your POST should really be another GET, as it doesn't change any data. Basic HTTP mechanisms works better that way (caching, proxying, ...). You'll have to add an attribute to your action method specifying that the answer depends on the arguments.
    – Philippe
    Commented Sep 25, 2016 at 13:47
  • thanks - so if I just specify 2 models at the top of the view - both will be filled as well as possible? Is this not adding redundancy and duplication? Can you elaborate on adding the attribute please don't follow - thanks.
    – niico
    Commented Sep 26, 2016 at 18:38
  • A view can have a single model, no more, which it receives from the return View(model) in the action method. MVC fills the action method's arguments by name from the values in the request.
    – Philippe
    Commented Sep 26, 2016 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.