Javascript 渲染局部视图时,不会将其设置为对象的实例

Javascript 渲染局部视图时,不会将其设置为对象的实例,javascript,c#,asp.net,asp.net-mvc,asp.net-core,Javascript,C#,Asp.net,Asp.net Mvc,Asp.net Core,我第一次尝试使用局部视图,我需要一些帮助。我将Javascript中的一个字符串发布到我的ASP控制器,以便我可以使用该字符串查询我的数据库。像这样: JavaScript function findEmployees(userCounty) { $.ajax({ type: "POST", url: '@Url.Action("Index", "Contact")', data:

我第一次尝试使用局部视图,我需要一些帮助。我将Javascript中的一个字符串发布到我的ASP控制器,以便我可以使用该字符串查询我的数据库。像这样:

JavaScript

function findEmployees(userCounty) {
    $.ajax({
        type: "POST",
        url: '@Url.Action("Index", "Contact")',
        data: JSON.stringify(userCounty),
        contentType: "application/json",
        error: function (e) {
            console.log(e)
            console.log("error")
        }
    });
}
控制器

    [HttpPost]
    public ActionResult Index([FromBody]string userCounty)
    {
        string county = userCounty.Substring(0, userCounty.IndexOf(" "));
        var query = from SOP in _context.SalesOffice_Plant
                    where SOP.County == county
                    select new SalesOffice_Plant
                    {
                        Employee = SOP.Employee
                    };

        return PartialView(query.ToList());
    }
    [HttpGet]
    public ActionResult Index()
    {
        ViewData["Title"] = "Contact Us";
        ViewBag.Current = "Contact";

        return View();
    }
当我设置断点时,我可以看到字符串被正确传递,我的LINQ查询工作正常。当我想在索引页中呈现员工表时,就会出现问题。页面加载后,JavaScript向控制器返回一个值。这意味着我需要一种“刷新页面”的方法。我被告知使用局部视图来解决这个问题,这就是我的想法

Index.cshtml

@model IEnumerable<Project.Models.SalesOffice_Plant>
//A bunch of Html
@await Html.PartialAsync("_IndexPartial")
//More Html
@model IEnumerable<Project.Models.SalesOffice_Plant>
<table class="table">
<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Employee)
            </td>
        </tr>
    }
</tbody>
@model IEnumerable
//一堆Html
@等待Html.partialSync(“\u IndexPartial”)
//更多Html
\u IndexPartial.cshtml

@model IEnumerable<Project.Models.SalesOffice_Plant>
//A bunch of Html
@await Html.PartialAsync("_IndexPartial")
//More Html
@model IEnumerable<Project.Models.SalesOffice_Plant>
<table class="table">
<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Employee)
            </td>
        </tr>
    }
</tbody>
@model IEnumerable
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.Employee)
}
理想情况下,我希望在Index.cshtml中生成并显示员工表。但是,当我加载得到的页面时,错误地告诉我@await Html.PartialAsync(“_IndexPartial”)”没有设置为对象的实例


任何指向正确方向的指针都会非常有用。

当您使用ajax时,它不会在后端代码完成后重新加载页面,因此您需要使用
.html()
方法将后端结果呈现为html

下面是一个完整的工作演示:

型号:

public class SalesOffice_Plant
{
    public int Id { get; set; }
    public string County { get; set; }
    public string Employee { get; set; }
}
视图(Index.cshtml):

结果:


这是否回答了您的问题?当文件扩展名存在时,HTML帮助程序引用的局部视图必须与调用该局部视图的标记文件位于同一文件夹中。我仔细阅读了那篇文章,我不知道它是否回答了我的问题。此外,my Index.cshtml和_IndexPartial.cshtml都位于同一个名为“Contact”的视图文件夹中。标记为
HttpGet
的contollection操作
Index
不会将任何模型值传递给其视图
索引<代码>索引
视图呈现尝试使用相同模型的局部视图\u IndexPartial。由于要索引的模型为空,因此会将该模型传递给导致异常的局部视图。您应该替换
返回视图()带有
返回视图(新列表())
[HttpPost]
public ActionResult Index([FromBody] string userCounty)
{
    string county = userCounty.Substring(0, userCounty.IndexOf(" "));
    var query = from SOP in _context.SalesOffice_Plant
                where SOP.County == county
                select new SalesOffice_Plant
                {
                    Employee = SOP.Employee
                };
    return PartialView("_IndexPartial", query.ToList());  //must specify the partial view name
                                                          //otherwise it will match the action name as partial view name
}
[HttpGet]
public ActionResult Index()
{
    ViewData["Title"] = "Contact Us";
    ViewBag.Current = "Contact";
    return View();
}