0
votes

Is there a way how to have template partial view, that would do just one job repeatedly but only one thing would be changing in the partial view so it could be used across multiple Views with different Models and not care about the Controller?

The Partial view would receive a bool from a View Model, depeding on the parent View. The bool from model would be used into the same positions always.

@Html.LabelFor(Model => Model.Bool1, new { @class = "class" }) 
@Html.RadioButtonFor(Model => Model.Bool1, true, new { @class = "class"})`

I would be passing the bool name into the partial view at the call of it.

@Html.Partial("_RadioButton", Model.Bool1})

Now, How do I do that to keep Bool changeable. As for example call the same partial in different View with different model as for example.

@Html.Partial("_RadioButton", Model.Bool2})

Thanks all.

1
That is what EditorTemplates are for - @Html.EditorFor(m => m.BoolProperty, "_RadioButton") and the partial will have @model bool with @Html.RadioButtonFor(m => m, true, new { @class = "class"}) etcuser3559349

1 Answers

0
votes

You should use an Editor template and pass a model based on the radio button into it.

Include the following line where you want to include the template, and pass in a model and any further variables (useful for small changes based on the location of the template).

 @Html.DisplayFor(m => Model, "_RadioButtonTmp", new { IsThisCorrect = "Yes", Title="RadioBtn" })

Then your template (_RadioButtonTmp) would be something like:

@model RadioButton
@if (ViewBag.IsThisCorrect == "Yes")
{
<p>However you want the radio button to look based on your model</p>
}

Update Alternative template

@model bool

<div>
@Html.RadioButtonFor(m => m, true, new { @class = "class"})
</div>