【发布时间】:2013-02-25 18:24:30
【问题描述】:
我一直在摆弄 MVC 很长一段时间,我有兴趣创建一个将所有 cd 内容记录在一张 cd 下的迷你应用程序。我现在的主要障碍是如何将内容列表与其他属性值一起传递给 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; }
}
查看:
$.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"/>
}
请注意,CdId 和 Description 值已经由 ajax 的 Serialize 函数传递 - 只有 Contents 属性是我无法理解这个想法的属性
更新
我通过创建一个将序列化数据发送到控制器的 ajax sn-p 解决了我的查询:
$.ajax({
type: 'POST',
url: 'http://localhost:54004/Cd/AddCd',
data: JSON.stringify(formData),
contentType:'application/json;charset=utf-8'
})
.success(function () { })
使用如下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" }]
};
现在控制器正在接收完整的 Cd 实体值及其内容。 希望这对将来的某人有用。
【问题讨论】:
-
干得好。我知道你去了哪里。
标签: c# javascript asp.net-mvc-3 jquery