You can post data as follows which is very simple:
var formData = $(this).serialize()
+ "&Id=" + Id
+ "&otherVal=" + otherVal;
$.post($(this).attr("action"), formData, function(res)
{
// do stuff with response
}, "json");
with an ActionMethod defined as
public ActionResult MyAction(int id, string otherVal)
{
return PartialView();
}
The model binder knows to match the parameter names you pass to the variables you want to bind to.
or you can use functionality like the following to extract the data from all Select dropdowns & Checkboxes on the Form:
// frm is $('form') that gets passed to the function
// includeContextObjects determines if it's only the elements in the
// current form, or from all over the page.
function GatherFormDataAndSubmit(frm, includeContextObjects)
{
var data = frm.serializeObject();
data = GetSelectData(frm, data, includeContextObjects);
data = GetCheckBoxData(frm, data, includeContextObjects);
$.post(frm.attr("action"), data, function(res)
{
UpdateSuccessMessage(res);
}, "json");
}
function GetSelectData(frm, data, includeContextObjects)
{
var objSelects;
if (includeContextObjects)
{
objSelects = $("select");
}
else
{
objSelects = frm.find("select");
}
if (objSelects.length)
{
data = GetDataObjectFromSelects(objSelects, data);
}
return data;
}
function GetDataObjectFromSelects(selects, data)
{
var valuesArray = selects.map(function()
{
return $.getAttributes($(this).find(":selected"));
});
var obj = new Array();
$.each(valuesArray, function(item) { obj.push($(this)[0]); });
if (!data)
{
data = {};
}
$.each(obj, function()
{
for (var propertyName in $(this)[0])
{
data[propertyName] = $(this).attr(propertyName);
}
});
return data;
}
function GetCheckBoxData(frm, data, includeContextObjects)
{
var objCheckBoxes;
if (includeContextObjects)
{
objCheckBoxes = $("input:checked");
}
else
{
objCheckBoxes = frm.find("input:checked");
}
if (objCheckBoxes.length)
{
data = GetDataObjectFromCheckBoxes(objCheckBoxes, data);
}
return data;
}
function GetDataObjectFromCheckBoxes(objCheckBoxes, data)
{
var valuesArray = objCheckBoxes.map(function()
{
return $.getAttributes($(this));
});
var obj = new Array();
$.each(valuesArray, function(item) { obj.push($(this)[0]); });
$.each(obj, function(i)
{
data["configuredFactsheets[" + i + "].configuredFactsheetId"] = $(this).attr("configuredFactsheetId");
});
return data;
}
Note, serializeObject() relies on this, and getAttributes relies on this
In this case, my Action Method is:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult EditConfiguredPanel(ConfiguredPanel cfp, List<ConfiguredFactsheet> configuredFactsheets)
{
bool success = repos.UpdateConfiguredPanel(cfp, configuredFactsheets);
return Json(success);
}
where the List<ConfiguredFactsheet>
's ids correspond to the ids gathered with GetCheckBoxData()
and the id of the ConfiguredPanel corresponds to the id gathered from the selected Select list option.
It wouldn't take too much to modify this to include Comments text and another Id value.
The Forms Authentication data will get transmitted as a cookie with every request, so yes.