开发者

serialize List<SelectListItem> from View to Controller in MVC 3

开发者 https://www.devze.com 2023-04-13 02:31 出处:网络
I\'m having issues passing my list object to my controller - using an ajax post. in the view I have this:

I'm having issues passing my list object to my controller - using an ajax post.

in the view I have this:

        try {
        var id = schedule.value;
        var data2 = @Html.Raw(Json.Encode(Model.SavedScheduleList));
        var url = '@Url.Action("SetActiveSchedule", "Frame")';
        $.post(url, { savedScheduleList: data2, signScheduleDataId: id }, function (data) { 
        });
    }
    catch (err) 
    {
        alert(err);
    }

My Contr开发者_如何学编程oller looks like this:

    [HttpPost]
    public ActionResult SetActiveSchedule(List<SelectListItem> savedScheduleList, int signScheduleDataId)
    {
        try
        {               
            return Json(null);
        }
        catch (Exception ex)
        {
            throw;
        }
    }

So when my Action is executed, my savedScheduleList contains a list object with 7 objects (which is the correct number of items I'm sending through. However each item seems to be "blank". ie, of the SelectListItem class, these are the property values for each item: Selected = false, Text = null, Value = null.

The Model class (which has been strongly typed to this view) is:

    public class ScheduleModel
{

    private SignSchedule activeSignSchedule = new SignSchedule();
    private List<SelectListItem> savedSignScheduleList = new List<SelectListItem>();

    public int SignDataId { get; set; }
    public ScheduleFrameList ListFrames { get; set; }
    public DateTime Start { get; set; }
    public LogMessage LogMessage { get; set; }
    public bool CommsLogVisible { get; set; }
    public SignSchedule SignScheduleToMakeActive { get; set; }
    public int ActiveSignScheduleId { get; set; }
    //public List<SignSchedule> SavedSignScheduleList { get { return savedSignScheduleList; } }
    public List<SelectListItem> SavedScheduleList { get { return savedSignScheduleList; } }
}

Examining data2 before the post, shows the correct data in Json format and examining the Request property within the Action I can see the correct values in the Form.AllKeys property as well as the Params property but it doesn't seem to correctly resolve it back to my parameter object of the Controller Action.

Is it possible what I'm trying to do?

Thanks

EDIT Here's a string representation of data2 variable:

var data2 = [{"Selected":false,"Text":"9","Value":"2589"},false,"Text":"afsdfs","Value":"2585"},false,"Text":"sdfas","Value":"2588"}....]

I'm just showing 3 items here but there is in fact 7 as expected.


The easiest way to send complex objects and lists is to use a JSON request, like this:

var id = schedule.value;
var data2 = @Html.Raw(Json.Encode(Model.SavedScheduleList));
$.ajax({
    url: url,
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ savedScheduleList: data2, signScheduleDataId: id }),
    traditional: true,
    success: function(result) {
        // TODO: do something with the results
    }
});

Note that the JSON.stringify function is natively built into modern browsers. But if you need to support legacy browsers you could include the json2.js script into your page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号