使用MVC5中的部分视图刷新DIV

时间:2016-11-02 18:56:58

标签: asp.net-mvc asp.net-mvc-5

我正在尝试根据下拉选项刷新div。我有一个使用局部视图(_Module)的主视图。主要观点有一个下拉列表;在选择下拉列表时,我正在尝试更改局部视图中的内容,而内容又将反映在主视图中。

主要观点:

@model Test.Models.CreateDirectoryModel 
@{
ViewBag.Title = "CreateAdmin";
}
@using (Html.BeginForm("CreateAdmin", "RDBWeb", FormMethod.Post))
{
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#ProductType").change(function () {
            var catType = $(this).val();
            $("#result").load('@(Url.Action("GetPPOFlag", "RDBWeb", null, Request.Url.Scheme))?catType=' + catType);
        });
    });
</script>
@Html.ValidationSummary(true)
<div class="form-horizontal">
    <h4>Create a New Directory</h4>
    <hr />
    <div class="form-group">
        @Html.LabelFor(model => model.DirectoryNumber, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DirectoryNumber)
            @Html.ValidationMessageFor(model => model.DirectoryNumber)
        </div>
    </div>
     <div class="form-group">
        @Html.LabelFor(model => model.ProductType, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
       @Html.DropDownListFor(model => model.ProductType, new SelectList(Model.ProductType), "Select Product")
       @Html.ValidationMessageFor(model => model.ProductType)
    </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.DirectoryName, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DirectoryName)
            @Html.ValidationMessageFor(model => model.DirectoryName)
        </div>
    </div>   
     <div class="form-group">
        @Html.LabelFor(model => model.AdminName, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
       @Html.DropDownListFor(model => model.AdminName, ViewBag.Drop as SelectList, "Select Admin")
       @Html.ValidationMessageFor(model => model.AdminName)
    </div>
    </div>
    <div id="result">
        @if(Model.Module != null)
        {
            {Html.RenderPartial("_Module", Model.Module);}
        }
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>

    <div>


    </div>
</div>  
}

部分视图是:

@model Test.Models.CreateDirectoryModel

@foreach (string s in Model.Module)
{
    @Html.LabelFor(a => s.ToString());
}

控制器中与这些相关的方法是:

    public ActionResult GetPPOFlag(string catType)
    {
        ProdTypeModel p1 = new ProdTypeModel();
        int flag = p1.GetPPOFlag(catType);
        return RedirectToAction("GetModule", new { flag = flag });
    }

    [HttpGet]
    public ActionResult GetModule(int flag)
    {
        ModuleType m1 = new ModuleType();
        List<string> moduleList= m1.GetModules(flag);
        return PartialView("_Module", moduleList);

    }

我的问题是,默认情况下,部分视图对象最初为null,因为尚未选择下拉值。我添加了一个检查,只有在对象不为null时才呈现局部视图;但是这不会将新值加载到局部视图中。我所看到的是调用GetModule方法并调用局部视图方法,但主页面上的div和局部视图没有变化。

1 个答案:

答案 0 :(得分:0)

尝试这样做,只需在主视图中保留一个空div,就像这样

 * Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 262-399-135
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more  information.
>>> 

在下拉选择更改时,提供ajax调用并获取部分视图并在div中呈现html

    <div id="result">

     </div>