1

I have a Strongly Types Partial View that I am trying to render from another Partial View

@{ Html.RenderPartial("Days", Model.DaysOpen, new ViewDataDictionary()); }

I have been folling advice based on the below ASP.NET MVC, strongly typed views, partial view parameters glitch

It allows me to create the parent(Centre) but does not create the Child (owned) Days entity.

Can anyone offer any advice

Details.cshtml

@model RCCMS.ObjectModel.Entities.Centre

@using (Html.BeginForm(null, null, FormMethod.Post, new { @class = "form-horizontal", Role = "form" }))
{
 @Html.ValidationSummary("Please correct the following errors:-")

<div id="HiddenFields">
    @Html.HiddenFor(m => m.RowVersion)
</div>

<fieldset>

    <div class="form-group">
        @Html.LabelFor(model => model.Name, new { @class = "col-md-2 control-label" })
        <div class="col-md-5">
            @Html.TextBoxFor(model => model.Name, new { @class = "form-control" })
        </div>
        <div class="col-md-1">
            @Html.ValidationMessageFor(model => model.Name, "*")
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Address1, new { @class = "col-md-2 control-label" })
        <div class="col-md-5">
            @Html.TextBoxFor(model => model.Address1, new { @class = "form-control" })
        </div>
        <div class="col-md-1">
            @Html.ValidationMessageFor(model => model.Address1, "*")
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Address2, new { @class = "col-md-2 control-label" })
        <div class="col-md-5">
            @Html.TextBoxFor(model => model.Address2, new { @class = "form-control" })
        </div>
        <div class="col-md-1">
            @Html.ValidationMessageFor(model => model.Address2, "*")
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Address3, new { @class = "col-md-2 control-label" })
        <div class="col-md-5">
            @Html.TextBoxFor(model => model.Address3, new { @class = "form-control" })
        </div>
        <div class="col-md-1">
            @Html.ValidationMessageFor(model => model.Address3, "*")
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Town, new { @class = "col-md-2 control-label" })
        <div class="col-md-5">
            @Html.TextBoxFor(model => model.Town, new { @class = "form-control" })
        </div>
        <div class="col-md-1">
            @Html.ValidationMessageFor(model => model.Town, "*")
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.County, new { @class = "col-md-2 control-label" })
        <div class="col-md-5">
            @Html.TextBoxFor(model => model.County, new { @class = "form-control" })
        </div>
        <div class="col-md-1">
            @Html.ValidationMessageFor(model => model.County, "*")
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Postcode, new { @class = "col-md-2 control-label" })
        <div class="col-md-5">
            @Html.TextBoxFor(model => model.Postcode, new { @class = "form-control" })
        </div>
        <div class="col-md-1">
            @Html.ValidationMessageFor(model => model.Postcode, "*")
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Uprn, new { @class = "col-md-2 control-label" })
        <div class="col-md-5">
            @Html.TextBoxFor(model => model.Uprn, new { @class = "form-control" })
        </div>
        <div class="col-md-1">
            @Html.ValidationMessageFor(model => model.Uprn, "*")
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Lunch, new { @class = "col-md-2 control-label" })
        <div class="col-md-5">
            @Html.TextBoxFor(model => model.Lunch, new { @class = "form-control" })
        </div>
        <div class="col-md-1">
            @Html.ValidationMessageFor(model => model.Lunch, "*")
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.VoluntaryContribution, new { @class = "col-md-2 control-label" })
        <div class="col-md-5">
            @Html.TextBoxFor(model => model.VoluntaryContribution, new { @class = "form-control" })
        </div>
        <div class="col-md-1">
            @Html.ValidationMessageFor(model => model.VoluntaryContribution, "*")
        </div>
    </div>



    @{ Html.RenderPartial("Days", Model.DaysOpen, new ViewDataDictionary()); }


    <div class="col-md-offset-2">
        <input type="submit" name="submit" value="Save" class="btn btn-primary"/>
        <input type="submit" id="cancel" name="submit" value="Cancel" class="btn"/>
    </div>

</fieldset>
}

And my Partial View (Days)

@model RCCMS.ObjectModel.Entities.Days

<div class="form-group">
@Html.Label("Days Open", new {@class = "col-md-5 control-label"})
<div class="col-md-10">
    <div class="checkbox-inline">
        <label>
            @Html.CheckBoxFor(m => m.Monday) Monday
            @Html.ValidationMessageFor(m => m.Monday, "*")
        </label>
    </div>
    <div class="checkbox-inline">
        <label>
            @Html.CheckBoxFor(m => m.Tuesday) Tuesday
            @Html.ValidationMessageFor(m => m.Tuesday, "*")
        </label>
    </div>
    <div class="checkbox-inline">
        <label>
            @Html.CheckBoxFor(m => m.Wednesday) Wednesday
            @Html.ValidationMessageFor(m => m.Wednesday, "*")
        </label>
    </div>
    <div class="checkbox-inline">
        <label>
            @Html.CheckBoxFor(m => m.Thursday) Thursday
            @Html.ValidationMessageFor(m => m.Thursday, "*")
        </label>
    </div>
    <div class="checkbox-inline">
        <label>
            @Html.CheckBoxFor(m => m.Friday) Friday
            @Html.ValidationMessageFor(m => m.Friday, "*")
        </label>
    </div>
    <div class="checkbox-inline">
        <label>
            @Html.CheckBoxFor(m => m.Saturday) Saturday
            @Html.ValidationMessageFor(m => m.Saturday, "*")
        </label>
    </div>
    <div class="checkbox-inline">
        <label>
            @Html.CheckBoxFor(m => m.Sunday) Sunday
            @Html.ValidationMessageFor(m => m.Sunday, "*")
        </label>
    </div>
</div>

The create action of the CentreController

public ActionResult Create()
{
        if (!Security.HasPemission(Permission.CanCreateCentres)) return View("AccessDenied");

        var model = new Centre();
        return (IsAjax()) ? (ActionResult)PartialView("Details", model) : View(model);
    }
Community
  • 1
  • 1
Frazer
  • 560
  • 2
  • 11
  • 21
  • 2
    What happens? null reference ? or no values? – WannaCSharp Nov 04 '13 at 13:18
  • Are you initialising your Model in your controller? Make sure RCCMS.ObjectModel.Entities.Centre is not null. – juju Nov 04 '13 at 13:31
  • Could you post the code of the HttpGet that passes the `Centre` model to this view? I want to make sure the `DaysOpen` property is not null. – Aage Nov 04 '13 at 13:31
  • I just get no values, the master data Centres is added, but days does not appear. the create controller action has var model = new Centre(); as shown in the new edit, the code works fine if you replicate the PartialView Code in Details.cshtml – Frazer Nov 04 '13 at 13:35
  • It seems that you are passing an empty model to your view? – WannaCSharp Nov 04 '13 at 13:41
  • Rather than `@{Html.RenderPartial(...)` try `@Html.Partial(...)` (no semi colon on end and no curly braces) - does this help? – SimonGoldstone Nov 04 '13 at 16:34
  • Ive tried various methods of @{Html... and @Html... but no difference. We have decided not to implement this idea at the moment and just duplicate the code but would still be interested in the result. – Frazer Nov 05 '13 at 15:24

0 Answers0