asp.net mvc 3 - Send model with list to controller via ajax -
i have task model:
public class task { public int id { get; set; } public string name { get; set; } public string description { get; set; } public datetime? duedate { get; set; } public int projectid { get; set; } public virtual icollection<useraccount> useraccounts { get; set; } ... }
i'd send task model via ajax controller:
function sendform(projectid, useraccountids, name, date, description, target) { $.ajax({ url: target, type: "post", contenttype: 'application/json', data: json.stringify({ projectid: projectid, useraccounts: useraccountids, name : name, duedate : date, description : description }), success: ajaxonsuccess, error: function (jqxhr, exception) { alert('es ist ein fehler bei der Übertragung aufgetreten.'); } }); }
this working fine there's big problem useraccounts. variable useraccountids integer array can't mapped icollection useraccounts of task model.
how able create useraccount object each id , map useraccounts collection? objects don't need more id.
how able create useraccount object each id , map useraccounts collection? objects don't need more id.
then need declare as:
public virtual ienumerable<int> useraccounts { get; set; }
if task
entity , not viewmodel best write viewmodel task since mentioned won't need other properties useraccount
other id
.
update: build useraccounts
in javascript:
var useraccounts = { { id: value1 }, { id: value2 } };
then include in post:
data: json.stringify({ projectid: projectid, useraccounts: useraccounts , name : name, duedate : date, description : description }),
that won't work if have dataannotation on useraccount model. , if it's mess have copy required fields of useraccount
view, put in hidden field (hacky) , send method.
Comments
Post a Comment