[英]Associate PartialView to model of the page
I have a situation I can't solve alone... I have this object: 我有一个我无法独自解决的情况...我有这个对象:
public class Service
{
...
public Configuration Conf{get; set;}
...
}
public class Configuration
{
...
public List<Gateway> Gateways{get; set;}
...
}
Now I have a page to create a new service and I want to add runtime (client-side) a partial view. 现在,我有一个页面来创建新服务,并且我想添加运行时(客户端)的局部视图。 I have a page that accept as model the Service class.. and a partial view that have the gateway as model.. Everything seems to work.. 我有一个页面可以接受Service类作为模型。还有一个局部视图以网关为模型。一切似乎都可以正常工作。
@model ObjectModel.Entities.Configurations.Service
...
@section scripts {
<script type="text/javascript">
function loadPartial(event) {
event.preventDefault();
event.stopPropagation();
var $div = $(event.target).closest(event.data.divContainer),
url = $(this).data('url'), model = event.data.model;
$.post(url, function (model) {
$div.prepend(model);
});
}
$('#link_add_gateway').live('click', { divContainer: "#new_gateway", model: @Html.Raw(Json.Encode(Model)) }, loadPartial);
</script>
}
...
<div id="new_gateway">
<a id="link_add_gateway" class="cursor_pointer"
data-url='@Url.Action("RenderGateway", "Configuration")'>Aggiungi gateway</a>
</div>
<input type="submit" value="Create" class="btn btn-default" />
And here the controller: 这是控制器:
//EDIT: Now service is valorized here too..
public ActionResult RenderGateway(Service service)
{
Gateway g = new Gateway();
service.Configuration.Gateways.Add(g);
return PartialView("~/Views/_Partials/Gateway/Edit.cshtml", g);
}
[HttpPost]
public ActionResult Create(Service service)
{
//Still nothing
}
Here the problem : Service has no gateway valorized.. I think is correct, but I don't know how to solve it! 这里的问题是 :服务没有网关。.我认为是正确的,但我不知道如何解决! I would like to associate the model of the partial view to the model of the page. 我想将部分视图的模型与页面的模型相关联。 How can I do? 我能怎么做?
thank you 谢谢
UPDATE: 更新:
public class Configuration
{
[XmlElement(ElementName = "gateway")]
public GatewaysList Gateways { get; set; }
public Configuration()
{
this.Gateways = new GatewaysList();
}
}
[Serializable]
public class GatewaysList : List<Gateway>
{
public Gateway this[int gatewayId]
{
get
{
return this.Find(g => g.GatewayId == gatewayId);
}
}
}
I think you shouldn't use get to do this call because you have to send parameters 我认为您不应该使用get来执行此调用,因为您必须发送参数
So try something like this 所以尝试这样的事情
$().ajax({method: 'POST', data: @Html.Raw(Json.Encode(Model)), //other parameters})
and then change 然后改变
public ActionResult RenderGateway(ObjectModel.Entities.Configurations.Service service)
{
return PartialView("~/Views/_Partials/Gateway/Edit.cshtml",service);
}
the key for your problem is to use @Html.Raw(Json.Encode(Model))
to reSend your Model over pages 您问题的关键是使用@Html.Raw(Json.Encode(Model))
在页面上重新发送模型
update 更新
this code came from my working project so I'm sure that works, try sending the parameter as a string and then deserialize it 该代码来自我的工作项目,所以我确定它可以正常工作,尝试将参数作为字符串发送,然后反序列化
public ActionResult RenderGateway(string service)
{
var jsSettings = new JsonSerializerSettings();
jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
var deserializedModel = JsonConvert.DeserializeObject<Service >(service, jsSettings);
//now deserializedModel is of type Service
return PartialView("~/Views/Shared/something.cshtml", deserializedModel);
}
update 2 更新2
I see from your GatewayList class that it's an indexer. 我从您的GatewayList类中看到它是一个索引器。 They can't be serialized by xmlserializer You can do something like this 它们不能被xmlserializer序列化。您可以执行以下操作
public class GatewaysList : List<Gateway>
{
[XmlIgnore]
public Gateway this[int gatewayId]
{
get
{
return this.Find(g => g.GatewayId == gatewayId);
}
}
[XmlArray(ElementName="GatewaysList")]
[XmlArrayItem(ElementName="Gateway", Type=typeof(Gateway))]
public List<Gateway> GatewaysList
{
get
{
}
set
{
}
}
}
Solved... "Just" 1 row missed in my code... In my PartialView: 解决了...在我的代码中,“仅” 1行在我的代码中丢失了...
@using (Html.BeginCollectionItem("Configuration.Gateways"))
Now everything works correctly.. 现在一切正常。
Ah... I have forgot to say that I had to install the BeginCollectionItem
plugin and add to web.config the following line: 啊...我忘了说我必须安装BeginCollectionItem
插件并将以下行添加到web.config中:
<add namespace="HtmlHelpers.BeginCollectionItem" />
in system.web.webPages.razor -> pages -> namespaces
在system.web.webPages.razor -> pages -> namespaces
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.