我有一个我无法单独解决的情况......我有这个对象:
public class Service
{
...
public Configuration Conf{get; set;}
...
}
public class Configuration
{
...
public List<Gateway> Gateways{get; set;}
...
}
现在我有一个页面来创建一个新服务,我想在运行时(客户端)添加局部视图。 我有一个接受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" />
这里是控制器:
//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
}
问题: 服务没有网关价值。我认为是正确的,但我不知道如何解决它!我想将局部视图的模型与页面模型相关联。 我该怎么办?
谢谢
更新:
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);
}
}
}
答案 0 :(得分:1)
我认为您不应该使用get来执行此调用,因为您必须发送参数
所以尝试这样的事情
$().ajax({method: 'POST', data: @Html.Raw(Json.Encode(Model)), //other parameters})
然后更改
public ActionResult RenderGateway(ObjectModel.Entities.Configurations.Service service)
{
return PartialView("~/Views/_Partials/Gateway/Edit.cshtml",service);
}
问题的关键是使用@Html.Raw(Json.Encode(Model))
在页面上重新发送模型
<强> 更新 强>
这段代码来自我的工作项目所以我确信它有效,尝试将参数作为字符串发送然后反序列化
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);
}
更新2
我从你的GatewayList类中看到它是一个索引器。它们无法通过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
{
}
}
}
答案 1 :(得分:0)
...解决 &#34;只是&#34;在我的代码中错过了1行...在我的PartialView中:
@using (Html.BeginCollectionItem("Configuration.Gateways"))
现在一切正常..
啊......我忘了说我必须安装BeginCollectionItem
插件并将web.config添加到以下行:
<add namespace="HtmlHelpers.BeginCollectionItem" />
system.web.webPages.razor -> pages -> namespaces
中的