Jquery 如何将Json数据返回到局部视图

Jquery 如何将Json数据返回到局部视图,jquery,ajax,asp.net-mvc-4,c#-4.0,Jquery,Ajax,Asp.net Mvc 4,C# 4.0,我有一个使用布局页面的索引页面。在索引页上,我有一个下拉列表和一个div部分 用于承载局部视图。我这样做是因为我计划有大约4个链接和4个链接 将加载不同的局部视图 <div class="selectOption1" id="custNum"> <label for="ddlCustomerNumber">Customer #:</label> <select id="ddlC

我有一个使用布局页面的索引页面。在索引页上,我有一个下拉列表和一个div部分 用于承载局部视图。我这样做是因为我计划有大约4个链接和4个链接 将加载不同的局部视图

         <div class="selectOption1" id="custNum">
               <label for="ddlCustomerNumber">Customer #:</label>
               <select id="ddlCustomerNumber" name="ddlCustomerNumber">
                 <option value="1001">1001</option>
                 <option value="1002">1002</option>
                 <option value="1003">1003</option>
                 <option value="1004">1004</option>    
               </select>
        </div>


        <div id="pageContent"></div>
问:我知道我可以从populatetextboxs方法返回partialview,但因为我已经返回了
这是不可能的。如何将此json数据返回到我的部分视图。

查看此链接-

              public ActionResult PopulateTextBoxes(Int32 customerId, string country)
                {
                    try
                    {
                        var relatedCustomerInfo = GetOtherCustomerInfo(customerId, country, sRadSelection);
                        return Json(relatedCustomerInfo, JsonRequestBehavior.AllowGet);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        return View("Error");
                    }

                }
下面是另一个例子:

Java脚本:

$.get( '@Url.Action("PopulateTextBoxes","Home", new { customerId = Model.ID } )', function(data) {
    $('#outputContainer').html(data);
}); 
public ActionResult PopulateTextBoxes(Int32 customerId, string country)
{
    try
    {
        var relatedCustomerInfo = GetOtherCustomerInfo(customerId, country, sRadSelection);
        return PartialView("~/Views/Home/_Partial.cshtml",relatedCustomerInfo);
    }
    catch (Exception ex)
    {
        logger.Error(ex);
        return View("Error");
    }
}
控制器:

$.get( '@Url.Action("PopulateTextBoxes","Home", new { customerId = Model.ID } )', function(data) {
    $('#outputContainer').html(data);
}); 
public ActionResult PopulateTextBoxes(Int32 customerId, string country)
{
    try
    {
        var relatedCustomerInfo = GetOtherCustomerInfo(customerId, country, sRadSelection);
        return PartialView("~/Views/Home/_Partial.cshtml",relatedCustomerInfo);
    }
    catch (Exception ex)
    {
        logger.Error(ex);
        return View("Error");
    }
}

可能需要对其进行一些调整,以使其完全按照您想要的方式工作,但您似乎对MVC非常了解,因此可以正确使用它

查看此链接-

下面是另一个例子:

Java脚本:

$.get( '@Url.Action("PopulateTextBoxes","Home", new { customerId = Model.ID } )', function(data) {
    $('#outputContainer').html(data);
}); 
public ActionResult PopulateTextBoxes(Int32 customerId, string country)
{
    try
    {
        var relatedCustomerInfo = GetOtherCustomerInfo(customerId, country, sRadSelection);
        return PartialView("~/Views/Home/_Partial.cshtml",relatedCustomerInfo);
    }
    catch (Exception ex)
    {
        logger.Error(ex);
        return View("Error");
    }
}
控制器:

$.get( '@Url.Action("PopulateTextBoxes","Home", new { customerId = Model.ID } )', function(data) {
    $('#outputContainer').html(data);
}); 
public ActionResult PopulateTextBoxes(Int32 customerId, string country)
{
    try
    {
        var relatedCustomerInfo = GetOtherCustomerInfo(customerId, country, sRadSelection);
        return PartialView("~/Views/Home/_Partial.cshtml",relatedCustomerInfo);
    }
    catch (Exception ex)
    {
        logger.Error(ex);
        return View("Error");
    }
}

可能需要对其进行一些调整,以使其完全按照您想要的方式工作,但您似乎非常了解MVC,因此可以正确地使用它。您的
for(var x=0;x
表明是这样的,但是您反复更新和覆盖元素值-这有什么意义?(或者您是否有包含多个元素且带有
id=“Customerfirstname”
等的无效html)?实际上,这是一个错误。这不是一个收藏。它应该只返回一条记录。循环不是必需的。你不清楚你想做什么。为什么需要同时返回json和html?不必返回json。我只是希望能够将数据返回到我的局部视图。我认为json是被推荐的。以它为例。进行ajax调用。设置relatedCustomerInfo并将其作为模型返回到部分视图-返回部分视图(“~/views/ABC/XXX.cshtml”,relatedCustomerInfo);。然后在ajax调用的成功部分,只需将返回的html绑定到controlsIs
relatedCustomerInfo
a集合之一。您的
for(var x=0;x
表明是这样的,但是您反复更新和覆盖元素值-这有什么意义?(或者您是否有包含多个元素且带有
id=“Customerfirstname”
等的无效html)?实际上,这是一个错误。这不是一个收藏。它应该只返回一条记录。循环不是必需的。你不清楚你想做什么。为什么需要同时返回json和html?不必返回json。我只是希望能够将数据返回到我的局部视图。我认为json是被推荐的。以它为例。进行ajax调用。设置relatedCustomerInfo并将其作为模型返回到部分视图-返回部分视图(“~/views/ABC/XXX.cshtml”,relatedCustomerInfo);。然后在ajax调用的成功部分,只需将返回的html绑定到其中一个控件,谢谢。在我自己的情况下,因为我有这个,它将是$('#pageContent').html(数据)是的,谢谢。在我自己的情况下,因为我有这个,它将是$('#pageContent').html(数据)