无法将局部视图渲染为视图的一部分

时间:2015-02-27 07:39:43

标签: c# asp.net-mvc-4

我在mvc4中提到部分视图时提到了几个问题。但我的问题是我的视图中有一个分区(DisplayRecords),并希望部分视图在该分区中打开,而不是点击“视图”按钮时的不同视图。

但这没有发生,因为在控制器中我返回局部视图,因为如果我返回视图,我无法传递包含数据的列表。 在返回视图时,它会抛出一个错误,即视图无法接受该类型的参数,如果没有传递列表,部分视图将无法工作。

我尝试将列表分配给模型属性,并将该属性值从客户端传递给部分视图,但会引发空引用异常。

有人可以建议我如何解决这个问题。

这是我写的:

我的观点:

@model mvcEmail.Models.EmailModel
@*<script type="text/javascript">
   
    var command = document.getElementById("FirstName");
        alert(command);
        if (command == "Value")
            $('#DisplayRecords').load('/Email/Index2');
  
    
</script>*@
@{
    ViewBag.Title = "Index";
}


<h2>Index</h2>
@using (@Html.BeginForm())
{
    <div>
        @* @Html.Label(Model.Acknowledge, new { id = "Acknowledge" })
        <br />*@
        @Html.TextBoxFor(model => model.FirstName, new { id = "FirstName", @class = "ClassName" })
        <br />
        @Html.TextBoxFor(model => model.PhoneNumber, new { id = "PhoneNumber", @class = "ClassPhoneNumber" })
        <br />

        <input type="submit" name="Command" value="Submit" />
        <input type="submit" name="Command" value="View" />
    </div>
    
    <div id="DisplayRecords">
        @if (ViewBag.query == "View")
        {
            @Html.Partial("Index2")           
        }
    </div>
    
    
}  

我的部分观点:

@model IEnumerable<mvcEmail.Models.EmailModel>

<p>
    @Html.ActionLink("Create New", "Index")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PhoneNumber)
        </th>

        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PhoneNumber)
            </td>

            @*  <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>*@
        </tr>
    }

</table>

我通过列表查看的控制器:

 if (Command == "View")
            {
                ViewBag.query = "View";
                //var records = new DisplayRecords();

                List<EmailModel> recs = new List<EmailModel>();
                cmd.CommandText = "DisplayRecords";
                cmd.Connection = con;
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    model = new EmailModel();

                    model.PhoneNumber = reader["phone"].ToString();
                    model.FirstName = reader["Name"].ToString();

                    recs.Add(model);
                }


                con.Close();
                con.Dispose();

                return PartialView("Index2", recs);

                //return View(recs);
            }

1 个答案:

答案 0 :(得分:0)

您的部分视图未获取其模型实例。您需要在视图中调用部分视图时提供模型 -

@Html.Partial("Index2", new List<mvcEmail.Models.EmailModel>(){this.Model})

或者我会建议编写另一个控制器动作,它将返回部分视图,模型为 -

public ActionResult PartialView()
        {
            List<mvcEmail.Models.EmailModel> emailListModel  = new List<mvcEmail.Models.EmailModel>();
            return PartialView(emailListModel);

        }

然后使用 -

调用它
@{Html.RenderAction("PartialView", "Controller_name",null);}

这将使您的代码更好,就像单一责任原则一样。

我希望这会对你有所帮助。

相关问题