0

I am loading the partial view based on dropdownlist value as suggested in the following links. I am able to show partial View and enter values in textboxes. But when I postback, I am unable to get values in Controller. I am getting all other values except this partial view values

Render Partial View Using jQuery in ASP.NET MVC

div id="divFloorPlans"></div>

 $('#ddlFloorPlans').change(function () {
        var numberOfFloorPlans = $(this).val();
        var data = { "id": numberOfFloorPlans };
        $.ajax({
            url: "FloorPlans",
            type: "POST",
            data: data, //if you need to post Model data, use this
            success: function (result) {                  
                $("#divFloorPlans").html("");
                $("#divFloorPlans").html(result);
            }
        });
    });

@model IList<ViewModels.FloorPlan>
@for (int i = 1; i <= Model.Count; ++i)
    {
        <div class="col-md-12" >               
            <div class="col-md-2" >
                @Html.DropDownListFor(m => m[i - 1].Bedrooms, ViewData["Bedrooms"] as List<SelectListItem> })
            </div>
            <div class="col-md-2" >
                @Html.DropDownListFor(m => m[i - 1].Bathrooms, ViewData["Bathrooms"] as List<SelectListItem> })
            </div>
            <div class="col-md-3" >
                @Html.TextBoxFor(m => m[i - 1].MinPrice})
                @Html.TextBoxFor(m => m[i - 1].MinPrice })
            </div>
            <div class="col-md-3">
                @Html.TextBoxFor(m => m[i - 1].MinSqFt, new { @placeholder = "From" })
                @Html.TextBoxFor(m => m[i - 1].MaxSqFt, new { @placeholder = "To"})
            </div>              
        </div>
    }

My Model looks like this.

  public class ItemEditVM
{
  public FloorPlan FloorPlan { get; set; }

    public IList<FloorPlan> ListFloorPlans { get; set; }
}

My Controllers

    //Partial View Returning Controller
    [HttpPost]
    public ActionResult FloorPlans(int id)
    {
        var model = new ItemEditVM();

        model.NumOfFloorplans = id;

        model.ListFloorPlans = new List<FloorPlan>();

        for (int i = 0; i < id; i++)
        {
            model.ListFloorPlans.Add(new FloorPlan { FloorPlanName = "", Bathrooms = "", Bedrooms = "", MaxPrice = "", MinPrice = "", MaxSqFt = "", MinSqFt = "" });
        }

        return View("FloorPlan", model.ListFloorPlans);
    }

    //Create Controller
    [HttpPost]
    public ActionResult Create(ItemEditVM model)
    {
        if (ModelState.IsValid)
        {

        }
    }
Community
  • 1
  • 1
Chatra
  • 2,989
  • 7
  • 40
  • 73
  • Try to add default constructor to `ItemEditVM`, with initialization `this.FloorPlan = new FloorPlanVM();` – Max Brodin Mar 15 '15 at 21:21
  • @MaxBrodin I added constructor. Now I am getting the property but not the values. Is there something I have to do in FloorPlanVM ? – Chatra Mar 15 '15 at 21:30
  • What is your controller method that returns the partial and method do you post to? –  Mar 15 '15 at 21:39
  • @StephenMuecke I updated post with controllers – Chatra Mar 15 '15 at 21:44
  • You are posting only `id` in the ajax so you can get only id in controller. As I understand you want to get access to `MinSqFt` etc. ?? – Piotr Leniartek Mar 15 '15 at 21:52
  • You partial is generating controls with `` and `` etc, but to bind to you model it needs to be `` etc. But why do you want to omit the last item in the collection with your use of `@for (int i = 1; i <= Model.Count; ++i)`? –  Mar 15 '15 at 21:52
  • @StephenMuecke How can Create , Is it in controller or View ? – Chatra Mar 15 '15 at 22:01
  • 1
    Your partial needs to be `@model ItemEditVM` with `@for (int i = 1; i <= Model.ListFloorPlans.Count; ++i) { @Html.TextBoxFor(m => m.ListFloorPlans[i - 1].MinPrice}) ..}` and the controller `return View("FloorPlan", model);` –  Mar 15 '15 at 22:07

1 Answers1

2

You partial views model is IList<FloorPlan> which is generating controls with

<input name="[0].MinPrice" ..>
<input name="[1].MinPrice" ..>

which would post back to IList<FloorPlan> model, but your method parameter is ItemEditVM model. You need to the partial view model to be @model ItemEditVM

In the GET method

return View("FloorPlan", model);

and in the view

@model ItemEditVM
@for (int i = 1; i <= Model.ListFloorPlans.Count; ++i)
{
    ....
    @Html.TextBoxFor(m => m.ListFloorPlans[i - 1].MinPrice})
    ....
}

which will generate the correct name attributes for binding to your model

<input name="ListFloorPlans[0].MinPrice" ..>
<input name="ListFloorPlans[1].MinPrice" ..>