首页 \ 问答 \ 如何将Json数据返回到局部视图(How to return Json data to a partial view)

如何将Json数据返回到局部视图(How to return Json data to a partial view)

我有一个使用布局页面的索引页面。 在索引页面上,我有一个下拉和一个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>

在html的脚本部分,我正在进行一个Ajax调用,它将获取jason数据,然后绑定在加载的局部视图中存在的控件上检索的字段。 这个简单的意思是,在我的部分视图中,我有用于customerfirstname,customerlastname和address的文本框。

         $('#ddlCustomerNumber').change(function () {         
                $.ajax({
                    url: '@Url.Action("PopulateTextBoxes", "Home")',
                    type: "GET",
                    data: {
                        "customerNumber": $(this).val(),
                        "Country": $("#divcountry").text().trim()
                    },
                    success: function (data) {
                        if (data != null) {
                            for (var x = 0; x < data.length; x++) {
                                $("#Customerfirstname").val(data[x].customerfirstname);
                                $("#Customerlastname").val(data[x].Customerlastname);
                                $("#Address").val(data[x].Address);                        
                            }
                        }
                    }
                });

            });

这里是让我获得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");
                    }

                }

问题:我知道我可以从我的PopulateTextBoxes方法返回partialview,但因为我已经返回json,所以这是不可能的。 我怎样才能返回这个JSON数据到我的局部视图。


I have an index page that uses a layout page. On the index page I have a dropdown and a div section for hosting partial views. I am doing it this way because I plan to have about 4 links and the 4 links will load different partial views.

         <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>

In the script section of the html I am making an Ajax call that will fetch jason data and then bind the fields retrieved on the controls that exist in my loaded partial view. This simple means that in my partial view I have textboxes for customerfirstname,customerlastname and address.

         $('#ddlCustomerNumber').change(function () {         
                $.ajax({
                    url: '@Url.Action("PopulateTextBoxes", "Home")',
                    type: "GET",
                    data: {
                        "customerNumber": $(this).val(),
                        "Country": $("#divcountry").text().trim()
                    },
                    success: function (data) {
                        if (data != null) {
                            for (var x = 0; x < data.length; x++) {
                                $("#Customerfirstname").val(data[x].customerfirstname);
                                $("#Customerlastname").val(data[x].Customerlastname);
                                $("#Address").val(data[x].Address);                        
                            }
                        }
                    }
                });

            });

Here is the method that gets me the json data that I plan to bind to controls in my partial view.

              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");
                    }

                }

Question: I know I can return a partialview from my PopulateTextBoxes method but because I am already returning json this is not possible. How can I return this json data to my partial view.


原文:https://stackoverflow.com/questions/37346666
更新时间:2023-05-31 12:05

最满意答案

不 - 你只需要把它写成:

class Beta(Alpha):
    def fie(self):
        super(Beta, self).fie()

请参阅:http: //yergler.net/blog/2011/07/04/super-self/ - 并从那里引用(因为它比我能解释得更好!):

根据Python 2.7.2标准库文档,super“return [s]一个代理对象,它将方法调用委托给父类或兄弟类的类型。”因此,在单继承的情况下,它委托对超类的访问,它不会返回超类的实例。 在上面的示例中,这意味着当您实例化B时,会发生以下情况:

enter B.__init__()
call super on B and call __init__ on the proxy object
enter A.__init__()
call super on self.__class__ and call __init__ on the proxy object

问题是,当我们进入第四步时,自我仍然引用我们的B实例,所以再次调用超级点回到A. 在技​​术方面:Ka-bloom。

在这篇文章中,链接到Raymond Hettinger的博客(他们总是值得一读): http//rhettinger.wordpress.com/2011/05/26/super-considered-super/

注意:阅读评论,其中用户建议使用type(self) (相当于你的self._ class _)以及为什么它不起作用


Nope - you just have to write it as:

class Beta(Alpha):
    def fie(self):
        super(Beta, self).fie()

See: http://yergler.net/blog/2011/07/04/super-self/ - and quoted from there (as it explains it better than I could!):

According to the Python 2.7.2 standard library documentation, super “return[s] a proxy object that delegates method calls to a parent or sibling class of type.” So in the case of single inheritance, it delegates access to the super class, it does not return an instance of the super class. In the example above, this means that when you instantiate B, the follow happens:

enter B.__init__()
call super on B and call __init__ on the proxy object
enter A.__init__()
call super on self.__class__ and call __init__ on the proxy object

The problem is that when we get to step four, self still refers to our instance of B, so calling super points back to A again. In technical terms: Ka-bloom.

And within that article is a link to a blog by Raymond Hettinger (and they're always worth reading): http://rhettinger.wordpress.com/2011/05/26/super-considered-super/

NB: read the comment where a user suggests using type(self) (equiv to your self._class_) and why it doesn't work

相关问答

更多
  • 不 - 你只需要把它写成: class Beta(Alpha): def fie(self): super(Beta, self).fie() 请参阅:http: //yergler.net/blog/2011/07/04/super-self/ - 并从那里引用(因为它比我能解释得更好!): 根据Python 2.7.2标准库文档,super“return [s]一个代理对象,它将方法调用委托给父类或兄弟类的类型。”因此,在单继承的情况下,它委托对超类的访问,它不会返回超类的实例 ...
  • Java有一个相当简单的继承模型:类必须有一个(并且只有一个)父类。 虽然可以实现多个接口,但不能从多个父接口继承,这可以看作Java的“多重继承”版本。 Java类中的大多数方法都是动态(后期)绑定的,但父类中声明为static , private和final的方法除外。 在Python中,正如您所指出的,您可以从多个(或没有)父级继承。 请注意,使用多重继承,您可以获得“ 钻石问题” 。 您应该知道Python如何解析它以及当您在Python中引用父类时它具有的含义(即:谁是您的父亲?) 在Python ...
  • 请通过https://github.com/greenlaw110/rythm/issues打开一个问题。 在确认并解决问题之前,请尝试使用不同的部分名称命名内部样式: @section(internal_section) { } 在您的base.html您有 @render(styles) { @render(internal_section) } Please open an issue at ...
  • 根据@chris我应该从Model实体中删除@Inheritance ,我也从UserModel删除了@SecondaryTable(name="USERS") ,它的工作非常完美。 according to @chris I should remove @Inheritance from Model entity and I also removed @SecondaryTable(name="USERS") from UserModel and it worked just perfectly.
  • 在Python中,您必须在test.__init__明确调用基类的__init__ test.__init__ : class test(base): def __init__(self): base.__init__(self) 或者,如果您希望支持多重继承,请使用super : class test(base): def __init__(self): super(test, self).__init__() 如果base.__init__看起来像 c ...
  • 继承和范围是两个完全不同的东西。 NewMenuItem是在New类的范围内定义的,在类File的范围内,但它继承自Menu ,它从object继承。 因此,虽然NewMenuItem只能通过类File和New来访问,但它将从Menu继承它的方法,而super将引用Menu 。 Inheritance and scope are two completely different things. NewMenuItem is defined inside the scope of the class New, ...
  • 你传入一个空字符串: Patent.__init__(self, CC, PN, KC="") 这就要求Patent.__init__()方法始终将KC设置为"" 。 取而代之,您KC任何价值: class USPatent(Patent): """"Class for holding information of uspto patents in Specific format""" def __init__(self, CC, PN, KC=""): Patent.__ ...
  • 委托是一种强大的机制,您可以将任务从一个类委托给另一个类。 这样做的主要优点是,您的某个类中的更改不会降级或转换为其他类。 此外,如果你没有得到这个原则,你的课程可能比他们应该做的更多。 通过这个我的意思是你得到一个类来做一些可能最好被封装到另一个中的东西,然后你可以使用委托来做同样的事情。 Delegation is a powerful mechanism where by you delegate a task away from one class to another. This has the ...
  • 这是一个有效的例子( http://jsfiddle.net/2n62J/ ) function interactiveElement() { } interactiveElement.prototype.draw = function(){ console.log('draw'); }; function projectile() { interactiveElement.call(this); } projectile.prototype = Object.create(inter ...