简体   繁体   English

通过Ajax向MVC发布复杂对象

[英]Posting Complex Object through Ajax to MVC

I have been fiddling with MVC for quite some time now and I'm interested in creating a mini-application that records all cd-contents under one cd. 我一直在摆弄MVC很长一段时间了,我有兴趣创建一个迷你应用程序,在一张CD下记录所有的CD内容。 My main hurdle for now is how can I pass on a list of Contents to a Cd.class along with other property values? 我现在的主要障碍是如何将内容列表与其他属性值一起传递给Cd.class?

public class Cd
{
    public int CdId { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Content> Contents { get; set; } 
}

public class Content
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ContentType { get; set; }
}

View: 视图:

 $.ajax({
 url: '/cd/addCd',
 type: 'POST',
 data:$('form').serialize()
    });

@using(Html.BeginForm()){
@Html.LabelFor(x=>x.CdId)
@Html.TextBoxFor(x=>x.CdId) <br/>
@Html.LabelFor(x=>x.Description)
@Html.TextBoxFor(x=>x.Description)<br />
<input type="submit" value="Submit" id="submit"/>

} }

Please do note that the CdId and Description values are already passed on by the Serialize function of ajax - only the Contents property is the one I'm having trouble grasping the idea 请注意,CdId和Description值已经被ajax的Serialize函数传递 - 只有Contents属性是我难以理解的想法

Update 更新

I solved my query by creating an ajax snippet that sends the serialized data to the controller: 我通过创建一个将序列化数据发送到控制器的ajax片段来解决我的查询:

 $.ajax({
            type: 'POST',
            url: 'http://localhost:54004/Cd/AddCd',
            data: JSON.stringify(formData),
            contentType:'application/json;charset=utf-8'
        })
        .success(function () { })

With the following formData obj: 使用以下formData obj:

   var formData = {
        'Description': "Learning Visual Studio 2012",
        'CdId': 1,
        'Contents': [{ "Id": 1, "Name": "Video #1", "ContentType": "Mp4" }, { "Id": 2, "Name": "Video #2", "ContentType": "Mp4" }]
    };

Now the controller is now receiving the full set of Cd entity value along with its Content. 现在,控制器现在正在接收完整的Cd实体值及其内容。 Hope this could serve useful to someone in the future. 希望这对未来的某些人有用。

Best way to achieve what you want is to remove the JQuery and replace your HTML.BeginForm with Ajax.BeginForm 实现你想要的最好的方法是删除JQuery并用Ajax.BeginForm替换你的HTML.BeginForm

@using(Ajax.BeginForm()){
@Html.LabelFor(x=>x.CdId)
@Html.TextBoxFor(x=>x.CdId) <br/>
@Html.LabelFor(x=>x.Description)
@Html.TextBoxFor(x=>x.Description)<br />

@Html.TextBoxFor(x=>x.Contents[0].Id)<br />
@Html.TextBoxFor(x=>x.Contents[0].Name)<br />
@Html.TextBoxFor(x=>x.Contents[0].ContentType)<br />
@Html.TextBoxFor(x=>x.Contents[1].Id)<br />
@Html.TextBoxFor(x=>x.Contents[1].Name)<br />
@Html.TextBoxFor(x=>x.Contents[1].ContentType)<br />
<input type="submit" value="Submit" id="submit"/>
}

Now your Submit button will post to a method Named after your View and Send the entire model 现在,您的“提交”按钮将发布到“查看并发送整个模型”后命名的方法

Instead of building the JSON object manually by iterating over, there is an interesting alternative in the form of knockoutmvc (Knockout integrated with MVC) 不是通过迭代手动构建JSON对象,而是以knockoutmvc的形式存在一个有趣的替代方案(Knockout与MVC集成)

Check the example - http://knockoutmvc.com/BetterList 查看示例 - http://knockoutmvc.com/BetterList

It sends a full JSON object without writing any extra code. 它发送一个完整的JSON对象,而无需编写任何额外的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM