asp.net-mvc-3 - 部分 View 呈现为完整 View

标签 asp.net-mvc-3 jquery partial-views

MVC 3、ajax、c#

我的部分 View 呈现为新页面,而不是替换搜索结果表。

Controller :

public class SearchController : Controller
{
    //
    // GET: /Search/
    private myEntities db = new myEntities();
    private Repository repo = new Repository();

    [HttpGet]
    public ActionResult Index()
    {
        var model = new List<PersonViewModel>();

        model = repo.GetPeople();

        return View(model);
    }




    public PartialViewResult _SearchResult(string fname, string lname)
    {
        var personResult = repo.GetSearchResult(fname, lname);

        return PartialView("_SearchResult", personResult);
    }

}

View :

<div class="page">

    <div class="middle-col-comment-mod">
                <h2>Search Existing Trespassers</h2>

        <div id="search">
            @using (Ajax.BeginForm("_SearchResult", "Search", null, new AjaxOptions { HttpMethod = "Post", InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace, UpdateTargetId = "indexSearchResults" }))
            {
                <div class="editor-field">   
                    <label>First Name:</label>
                    @Html.TextBox("FirstName")

                    <label style = "margin-left: 15px;">Last Name:</label>
                    @Html.TextBox("LastName", "", new { style = "margin-right: 15px;" })

                    <input type="submit" name="submit" class="skbutton" value="Search" />

                </div>

            }

        </div>

                    <table id="indexSearchResults" class="data-table">
                        <tr>
                            <th>
                                FirstName
                            </th>
                            <th>
                                LastName
                            </th>
                            <th>
                                Gender
                            </th>
                            <th>
                                City
                            </th>
                            <th>
                                DOB
                            </th>
                            <th>
                                IsStudent
                            </th>
                            <th>
                                Actions
                            </th>
                        </tr>

                    @if (Model.Count() == 0)
                    {
                          <tr>
                            <td colspan=7>
                                There are currently no trespassers in the trespass database.
                            </td>
                          </tr>
                    }
                    else
                    {
                    foreach (var item in Model)
                    {
                        <tr>
                                 <td>
                                @Html.DisplayFor(modelItem => item.FirstName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.LastName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Gender)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.DOB)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.School)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.IsStudent)
                            </td>

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

            </div>
    </div>

以及部分 View :

@model IEnumerable<TrespassTracker.Models.PersonViewModel>

<table>
    <tr>
        <th>
            First Name
        </th>
        <th>
            Last Name
        </th>
        <th>
            Gender
        </th>
        <th>
            Date of Birth
        </th>
        <th>
            Is a Student?
        </th>
        <th>
            Actions
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Gender)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DOB)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsStudent)
        </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>

        <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

根据我的研究,我发现通常的嫌疑是部分中缺少 jquery 脚本的链接,但我有这个。我检查了我的婚礼开发工具上的网络选项卡,发现它正在被调用。还有什么问题可能吗?

最佳答案

脚本不会引起任何问题,我在这里看到的唯一错误是表格。您正在尝试用返回表的其他表填充该表,请使用 div 来更新 ajax 表单(搜索结果):

<div id="indexSearchResults">
   <table>
     ..........
   </table>
</div>

关于asp.net-mvc-3 - 部分 View 呈现为完整 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16045967/

相关文章:

asp.net-mvc-3 - SignalR 0.5.2 - Context.User 在断开连接时为空

c# - 如何在 mvc3 web 应用程序中设置特定的路由

asp.net-mvc-3 - 将 mvc 下拉列表绑定(bind)到基本列表

javascript - 将 AJAX 加载程序附加到各个链接上的菜单/导航栏

jquery - 下拉菜单无法正常工作

jquery - ASP MVC3 - 使用部分 View 将新的 HTML 元素附加到页面

javascript - 外部化脚本时如何在 javascript 中生成正确的 URL

javascript - Uncaught ReferenceError : testString is not defined

asp.net-mvc - MVC中局部 View 和用户控件的区别

asp.net-mvc-3 - 如何在 MVC 中使用存储库模式创建动态的多个局部 View