Home MVC, post partial model
Reply: 0

MVC, post partial model

user10714 Published in September 19, 2018, 6:43 pm

This question already has an answer here:

  • Post an HTML Table to ADO.NET DataTable 2 answers

I have object A which has a List<ObjectB> ObjectB has several properties. Id, Mandatory, Name etc.

So I return a viewmodel (ObjectA) and have this in my razor:

@model ObjectA
<div>@Html.HiddenFor(m => ObjectA.ObjectC.ID)
    <dl class="dl-horizontal">
            @Html.DisplayNameFor(model => ObjectA.ObjectC.Name)

            @Html.DisplayFor(model => ObjectA.ObjectC.Name)
    // display stuff from objectA
    @using (Html.BeginForm())
        foreach (var ft in ObjectA.ObjectB)
            @Html.HiddenFor(c => ft.ID)
            <div class="row">
                <div class="col">
                @if (ft.Mandatory)
                    @Html.CheckBoxFor(c => ft.Mandatory, new { id = ft.ID, disabled = "disabled" })
                    @Html.HiddenFor(c => ft.Mandatory)
                    @Html.CheckBoxFor(c => ft.Mandatory, new { id = ft.ID })

and in my Controller I tried as input parameter: List<ObjectB> items but it was null. Now I know I could try FormCollection which I did and found out that form.Get("ft.id") had the amount of items in de objectB list. same for mandatory. But I'd like it strong typed. Either:

1 object A with all subobjects of type B

2 a list/ienumerable of type objectB.

It's probably a small thing, but I can't see it right now.

edit my model:

public class ObjectA : BaseViewModel
    public ObjectC DisplayOnly { get; internal set; }

    public List<ObjectB> Features { get; set; }

My view: (see above) My controller:

public ActionResult Details(ObjectA vm)
    if (ModelState.IsValid)
        int hid = Convert.ToInt32(RouteData.Values["id"]);
share|improve this question

marked as duplicate by Stephen Muecke asp.net-mvc StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var $hover = $(this).addClass('hover-bound'), $msg = $hover.siblings('.dupe-hammer-message'); $hover.hover( function() { $hover.showInfoMessage('', { messageElement: $msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Nov 12 '17 at 20:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • When you post your model ObjectA, at Html level I don't see any reason for the browser to send all the fields but only those present in the Html form. So it doesn't make much sense trying to receive List<ObjectB> as input in your controller. – derloopkat Nov 12 '17 at 20:08
  • All props of ObjectA are just there to display. And the ObjectB is a sort of extended select list. It's id, mandatory, name, price, description etc. So only the changes of objectB is needed. But a full objectA with everything is okay too. Tried that and was null. – JP Hellemons Nov 12 '17 at 20:11

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331015 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO