简体   繁体   中英

Working with multiple partial views

Good day

I am working with 7 partial views on my index page.

Index.cshtml (code section that creates the partial views in their own tabs)

@model project1.Models.MasterModel
<div class="tab-content col-lg-9 col-md-9 col-sm-9 col-xs-9" style="margin-bottom:110px;">
<div id="partial1" class="tab-pane fade in active">
    @Html.Partial("partial1", Model)
</div>
<div id="partial2" class="tab-pane fade in">
    @Html.Partial("partial2", Model)
</div>
<div id="partial3" class="tab-pane fade in">
    @Html.Partial("partial3", Model)
</div>
<div id="partial4" class="tab-pane fade in">
    @Html.Partial("partial4", Model.Owners)
</div>
<div id="partial5" class="tab-pane fade in">
    @Html.Partial("partial5", Model)
</div>
<div id="partial6" class="tab-pane fade in">
    @Html.Partial("partial6", Model)
</div>
<div id="partial7" class="tab-pane fade in">
    @Html.Partial("partial7", Model)
</div>

On partial1 I have a submit button that should populate the other partial views with the information that is submitted on the first partial view.

My Model consist of 7 models (one for each partial view)

MasterModel.cs

public class MasterModel
{
    public DisplayPartial1 view1 {get;set;}

    public DisplayPartial2 view2 {get;set;}

    public DisplayPartial3 view3 {get;set;}

    public DisplayPartial4 view4 {get;set;}

    public DisplayPartial5 view5 {get;set;}

    public DisplayPartial6 view6 {get;set;}

    public DisplayPartial7 view7 {get;set;}
}

In my controller's Index Action result I create a new instance of the MasterModel and then I create new instances for all the sub models and return to the Index view.

MainController.cs

// Global variable used and returned by all partial views
MasterModel main = new MasterModel();

public ActionResult Index()
{
    MasterModel master = new MasterModel();
    master.view1 = new DisplayPartial1 ();
    master.view2 = new DisplayPartial2 ();
    master.view3 = new DisplayPartial3 ();
    master.view4 = new DisplayPartial4 ();
    master.view5 = new DisplayPartial5 ();
    master.view6 = new DisplayPartial6 ();
    master.view7 = new DisplayPartial7 ();
    return View(master);
}

public ActionResult MyPartial1()
{
    return PartialView("MyPartial1", master);
}

[HttpPost]
public ActionResult MyPartial1(MasterModel model, string submitButton)
{
    if(submitButton == "GetInfo")
    {
        /*Process input values*/
        //wcf service return data
        main.view1.id = serviceObject.Id;
        ...
        main.view4.name = serviceObject.Name;
        ...
    }
    return PartialView("MyPartial1", master);
}

When MyPartial1 is returned then the other 6 partial view do not update with the assigned information until I do a submit call with a java script that looks like the following:

<script type="text/javascript">
    $(document).ready(function () {
        $('#input1').change(function () {
            $('form').submit();
        });
        $('#input2').change(function () {
            $('form').submit();
        });
    });
</script>

An Example Partial view (all the partial views work on the same principals)

@model OperatorLicense.Models.MasterModel
@using (Ajax.BeginForm("MyPartial1", "Operator", null, new AjaxOptions
{
    UpdateTargetId = "MyPartial1",
    InsertionMode = InsertionMode.Replace,
    HttpMethod = "Post"
}, new { id = "MyPartial1" }))
{
    @Html.TextBoxFor(model => model.view1.SerialNumber, new { id="SerialNumber", @class = "form-control", placeholder = Html.DisplayNameFor(n => n.view1.SerialNumber) })
    ...
}

Is there a way to refresh the other partial views when the MyPartial1 partial view has returned so that the assaigned values can be displayed on the required partial view?

You can use OnSuccess or OnComplete property of AjaxOptions class.

@using (Ajax.BeginForm("MyPartial1", "Operator", null, new AjaxOptions
{
    UpdateTargetId = "MyPartial1",
    InsertionMode = InsertionMode.Replace,
    HttpMethod = "Post",
    OnSuccess="partial1Success"
}, new { id = "MyPartial1" }))
{
    @Html.TextBoxFor(model => model.view1.SerialNumber, new { id="SerialNumber", @class = "form-control", placeholder = Html.DisplayNameFor(n => n.view1.SerialNumber) })
    ...
}

define partial1Success method in javascript

<script type="text/javascript">
    function partial1Success(){
       // write your other partial view refresh logic here
    }
</script>

partial1Success method called after from submit successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM