【发布时间】:2015-02-14 16:33:10
【问题描述】:
我有一个无法单独解决的情况......我有这个对象:
public class Service
{
...
public Configuration Conf{get; set;}
...
}
public class Configuration
{
...
public List<Gateway> Gateways{get; set;}
...
}
现在我有一个创建新服务的页面,我想添加运行时(客户端)一个局部视图。 我有一个页面接受服务类作为模型.. 和一个以网关为模型的部分视图.. 一切似乎都正常..
@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
}
问题出在这里: Service has no gateway valorized..我认为是正确的,但我不知道如何解决它!我想将局部视图的模型与页面的模型相关联。 我该怎么办?
谢谢
更新:
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);
}
}
}
【问题讨论】:
-
你试过我更新的答案了吗?
标签: c# jquery model-view-controller model runtime