I have one view model and i’m pass that view model into controller, but one of the model property is a list of other class. so i’m not able to bind it via jQuery.
I have the following view model.
JavaScript
x
8
1
public class ToolsAddViewModel
2
{
3
public string Tools_Name { get; set; }
4
public string Tools_Desc { get; set; }
5
public int Category_ID { get; set; }
6
public List<ToolsParamsBlockViewModel> Params_List { get; set; }
7
}
8
ToolsParamsBlockViewModel class that is used as list type
JavaScript
1
7
1
public class ToolsParamsBlockViewModel
2
{
3
public int Params_ID { get; set; }
4
public string Params_CSS_Attribute { get; set; }
5
public int Params_Priority { get; set; }
6
}
7
here is my controller method that handle viewmodel data
JavaScript
1
7
1
[HttpPost]
2
public ActionResult Manage(ToolsAddViewModel toolsAddViewModel)
3
{
4
//insert viewmodel data into database
5
return RedirectToAction("Index", "Tools");
6
}
7
and finally im trying to add data into viewmodel using jQuery, here it is. im use table for add list into Params_List
property.
JavaScript
1
22
22
1
$("#btnSave").on("click", function () {
2
var ParamsList = [];
3
$('#paramsBlockTable tbody > tr').each(function () {
4
var SingleParams = [];
5
$(this).find("input,select").each(function () {
6
SingleParams.push($(this).val());
7
console.log(values);
8
});
9
ParamsList.push(values);
10
});
11
var ToolsModel = {
12
"ID": $("#ID").val(),
13
"Tools_Name": $("#Tools_Name").val(),
14
"Category_ID": $("#Category_ID").val(),
15
"Params_List": ParamsList,
16
"ScriptFiles_IDs": $("#ScriptFiles_IDs").val(),
17
"Tools_SEO_Keyword": $("#Tools_SEO_Keyword").val(),
18
"Tools_Desc": $("#Tools_Desc").val(),
19
}
20
console.log(ToolsModel);
21
});
22
here in ParamsList
have array of table row elements but i need it into view model format.
thanks in advance
Advertisement
Answer
thanks phuzi its work for me 🙂
here I have changed some code block.
JavaScript
1
23
23
1
$("#btnSave").on("click", function () {
2
var ParamsList = [];
3
$('#paramsBlockTable tbody > tr').each(function () {
4
let SingleParams = {
5
Params_ID: $(this).find(".params-id").val(),
6
Params_CSS_Attribute: $(this).find(".params-attribute").val(),
7
Params_Priority: $(this).find(".params-priority").val()
8
}
9
ParamsList.push(SingleParams);
10
});
11
12
var ToolsModel = {
13
"ID": $("#ID").val(),
14
"Tools_Name": $("#Tools_Name").val(),
15
"Category_ID": $("#Category_ID").val(),
16
"Params_List": ParamsList,
17
"ScriptFiles_IDs": $("#ScriptFiles_IDs").val(),
18
"Tools_SEO_Keyword": $("#Tools_SEO_Keyword").val(),
19
"Tools_Desc": $("#Tools_Desc").val(),
20
}
21
console.log(ToolsModel);
22
});
23