加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 具有嵌套视图模型和Knockout的ASP.NET MVC

发布时间:2020-12-16 03:45:02 所属栏目:asp.Net 来源:网络整理
导读:无法让我的大脑围绕如何为以下ASP.NET MVC 4嵌套视图模型实现knockout: public class MyProfile{ public string Name { get; set; } public IListVM1 List1 { get; set; } public IListVM2 List2 { get; set; } .... public IListVM10 List10 { get; set; }
无法让我的大脑围绕如何为以下ASP.NET MVC 4嵌套视图模型实现knockout:

public class MyProfile
{
  public string Name { get; set; }

  public IList<VM1>   List1    { get; set; }
  public IList<VM2>   List2    { get; set; }
  ....
  public IList<VM10>  List10    { get; set; }
}
// example of VM view model
public class VM1
{
   IList<Label> Labels { get; set; }
   IList<Contact1> Contact1 { get; set; }
}

在视图中我接受这样的模型:

@model MyProfile

@using (Html.BeginForm("Index","Profile",FormMethod.Post,new { id = "profileEditorForm" }))
{

  @Html.ValidationSummary(false)
    <fieldset>
  <legend>User's data</legend>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" class="required" data-bind="value: Name"/>
    </fieldset>

 @Html.EditorFor(m => @Model.List1,"List1") @* Editpr model for List1*@
 @Html.EditorFor(m => @Model.List2,"List2")
 .....
 @Html.EditorFor(m => @Model.List10,"List10")

 <p>
   <input type="submit" value="Save" data-bind="enable: (List1().length > 0) && (List2().length > 0) && ...(List10().length > 0)" />
<a href="/">Cancel</a>
 </p>
}

List1的EditorTemplate看起来像这样,有多个问题:

@model IList<FiveW.ViewModels.List1>

<fieldset>

  <table>
    <tbody data-bind="foreach: Contact1">
      <tr>
        <td>
          <label>Email:</label></td>
        <td>
          @* How do you put combobox here with labels here?
          How do you tie selected label to selected property on your Contact1 object *@
          @*<select data-bind="options: Labels,optionsText: 'LabelName',value: selectedLabel,optionsCaption: 'Choose...'"></select></td>
        <td>
          <input type="text" data-bind="value: Name,uniqueName: true" class="required" /></td>
        <td>
          <a href="#" data-bind="click: function() { viewModel.removeContact1(this); }">Delete</a></td>
      </tr>
    </tbody>
  </table>


  <button data-bind="click: addContact1">Add Contact1</button>

</fieldset>

编辑

除了验证逻辑之外,VM1到VM10是相同的,所以我必须让它们成为不同的类(不幸的是,因为它在模型和视图中重复了很多).

客户方 – 这就是我要问的问题:
我需要传递包含嵌套列表的ASP MVC模型,并在客户端上使用knockout(我发现它最适合动态列表).
它类似于gmail联系人 – 你有家庭/工作/移动/传真电话 – 所以一个列表是手机的标签(它是什么手机),应该显示为组合框,另一个是可以增加的动态手机列表根据用户点击.

结束编辑

>我不明白如何从这个嵌套模型创建一个knockout viewModel,显然Name应该是它的一部分,但其余的是列表,它们也包含列表.
>如何映射?
>如何处理它(一个进入下拉列表,它将是另一个列表的标签,长度可变 – 这是在这里使用knockout的唯一原因).
>一旦填写完毕,如何将它们放在一起并运回控制器动作?
>当标签是名称标签的下拉列表(或组合框)时,如何编写该编辑器模型(例如:[label] home / work [name] email,[label] mobile / home / car [name] phone)

如果它是IList里面的简单类 – 它就像here.问题是列表中有列表,Knockout要求一切都是可观察的,不知道如何在java脚本中表达这个嵌套模型.

请帮忙.提前致谢!

解决方法

我不会使用映射.我只是像这样声明ViewModel客户端:

//I'm asuming the properties for Label and Contact,this is just for example purposes
function LabelViewModel(){
    var self = this;
    self.LabelName = ko.observable();
}
function Contact(){
    var self = this;
    self.Name = ko.observable();
    self.LastName = ko.observable();
}

//This is the definition for the List. I believe it shouldn't matter that the class names are different as long as the structure is the same
function ListViewModel(){
    var self = this;
    self.Labels = ko.observableArray();
    self.Contacts = ko.observableArray();
}

function MainViewModel(){
    var self = this;
    self.Name = ko.observable();
    self.List1 = ko.observableArray();
    //....
    self.List10 = ko.observableArray();
}


$(document).ready(function(){
    var viewModel = new MainViewModel(@Html.Raw(JsonConvert.SerializeObject(Model)));
    ko.applyBindings(viewModel);
});

然后,在提交时,我会尝试从jquery提交,而不是直接进行http post:

var viewModelJs = ko.toJS(ko.utils.unwrapObservable(viewModel));
var parameters = JSON.stringify({vm: viewModelJs});

$.ajax('http://yourControllerUrlHere',{
    data: parameters,type: "POST",contentType: "application/json",dataType: "json",success: function (result) {
        console.log('Submitted ok');
    },error: function (error) {
        console.log(error);
    }
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读