简体   繁体   English

如何从返回的局部视图访问数据以及JSON数据

[英]How to access data from a return partial view along with JSON data

I use: 我用:

$.get('/Main/Sub', { id: id }, function(response, text, XMLHttpRequest){
    // returns the page
});

C# controller: C#控制器:

public ActionResult Sub(int id){
    // code
return PartialView(ViewNames.Sub, theViewModel);
}

What I'm trying to do is return the partial ( which it does ) along with view model data. 我正在尝试做的是返回部分视图(它所做的)以及视图模型数据。 Currently there isn't a way for me to decipher the data being passed back in the theViewModel, I can only read the html page being returned. 目前,我没有办法解密在theViewModel中传递回的数据,我只能读取返回的html页面。 How can I access the data as well? 我也该如何访问数据?

Your partial view could have a bunch of hidden fields: 您的局部视图可能包含一堆隐藏字段:

@* Can target directly or use classes to target hidden fields in JS *@
@Html.HiddenFor(m => m.ID, new { @class = "id-field" })
@Html.HiddenFor(m => m.Name, new { @class = "name-field" })

Or your partial view could have data attributes on it: 或者您的局部视图可能具有数据属性:

<div data-row="{@JsonConvert.SerializeObject(Model)}"

or 要么

<div data-id="@Model.ID" data-name="@Model.Name"

These are a few approaches. 这些是一些方法。 Alternatively, return JSON, and bind the JSON to a template like these: 或者,返回JSON,并将JSON绑定到如下模板:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM