【问题标题】:Associate PartialView to model of the page将 PartialView 关联到页面模型
【发布时间】: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


【解决方案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
          {

          }
       }
}

【讨论】:

【解决方案2】:

解决了... “只是”在我的代码中遗漏了 1 行...在我的 PartialView 中:

@using (Html.BeginCollectionItem("Configuration.Gateways"))

现在一切正常..

啊...我忘了说我必须安装 BeginCollectionItem 插件并在 web.config 中添加以下行:

<add namespace="HtmlHelpers.BeginCollectionItem" />

system.web.webPages.razor -&gt; pages -&gt; namespaces

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2021-10-22
    • 2014-03-25
    • 1970-01-01
    • 2017-03-23
    相关资源
    最近更新 更多