0

I have 4 Action results

public ActionResult Parent1 ()
{
    ViewModel VM = new ViewModel ();      
    return View(VM);
}

[HttpPost]
public ActionResult Parent1(ViewModel VM)
{           
    return View(VM);
}

[ChildActionOnly]
public ActionResult PortfolioComposition(ViewModel VM)
{
    string UserId = "UserId1";
    VM.PortfolioUserName = "ac";
    VM.PList = new List<PortfolioBar>();
    PortfolioBar bar = new PortfolioBar();
    bar.Category = "a";
    bar.InstrumentName = "a";
    bar.Weight = "c";
    bar.XyzStats = "d";
    vm.PortBarList.Add(bar);
    return View(vm);
}

[ChildActionOnly]
public ActionResult Partial2(ViewModel VM)
{
    return View(VM);
}

In view Model

public class ViewModel
{
    public String PortfolioUserName { get; set; }
    public List<PortfolioBar> PortBarList = new List<PortfolioBar>();
}

public class PortfolioBar
{
    public string InstrumentName { get; set; }
    public string Category { get; set; } 
    public string Weight { get; set; } 
    public string XyzStats { get; set; }
}

Parent View

@using (Html.BeginForm())
{
    @{ Html.RenderAction("Partial1", "Home", new { vm = Model }); }
    <div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Search" class="btn btn-default" />
    </div>
}  

Partial1 View

@foreach (var item in Model.listvm)
{
    @{ Html.RenderAction("Partial2", "Home", new { vm = Model }); }
}

partial2 View

@TextBoxFor(m => m.weight)

Everything is public in above code. I want to get value of weight on postback to parent controller. I don't know how to do it. May be I am doing something wrong. Is it fine that I am doing it with same object? or do I need to use another object ? But then how will I merge those two objects on post back?

User
  • 17
  • 1
  • 6
  • 1
    The code in your question is full of typos and would not even compile. Copy the real code here. –  Jun 16 '16 at 07:11
  • Sorry for previous code, this one is much cleaner – User Jun 16 '16 at 07:20
  • You cannot use partials to generate form controls for collections. You need to use and `EditorTemplate` for typeof `PortfolioBar` or you need to use a `for` loop in the view (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) for examples). And `PortBarList` is a field and will not be bound anyway - it needs to be a property (add `{ get; set; }` –  Jun 16 '16 at 07:24
  • Hey, I tried to use get and set, then I got almost what I want but I only got list and all others fields were null. I'll try editor templete now – User Jun 16 '16 at 07:37
  • They were not bound because every textbox your creating has `name="weight"` but to bind to a collection it would need to be `name="PortBarList[0].weight"`, `name="PortBarList[1].weight"` etc –  Jun 16 '16 at 07:40
  • Actually Name is in ViewModel and not in list, there will be only one name for complete form. PortfolioUserName – User Jun 16 '16 at 07:47
  • What? I'm referring to the `name` attributes of the html your generating. And why are you using `RenderAction()` anyway - it makes no sense here –  Jun 16 '16 at 07:50
  • Should I use render partial ? I was talking about this field class ViewModel public String PortfolioUserName { get; set; } – User Jun 16 '16 at 07:52
  • No. The model in you parent view is already `ViewModel` so calling another method to render a partial of a `ViewModel` is pointless. Just populate the view model in the `Parent1` method, including the values for the collection, then generate the form controls for the collection using a `for` loop or `EditorTemplate` as per the link I gave you. –  Jun 16 '16 at 07:55

0 Answers0