I have the following simple class with student firstname and lastname properties. When the view for this model loads, I want to add additional columns to assign cost related information for each row, so that I added two fields in my DOT class i.e. StudentDoaminModel.
The views loads ok, but the value for each of those new properties, Cost1 and Cost2 doesn't pass to controller.
I want to use ajax to pass the model to the controller
Student Model class
public class Student
{
public string first_name{ get; set; }
public string last_name { get; set; }
}
Student Domain Model class
public class StudentDomainModel
{
public string first_name{ get; set; }
public string last_name { get; set; }
public int Cost1 { get; set; }
public Decimal Cost2 { get; set; }
}
view
<table id="tblProducts">
<thead class="text-light bg-dark">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Cost1</th>
<th>Cost2</th>
</tr>
</thead>
<tbody>
@if (Model != null)
{
for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.DisplayFor(m => m[i].first_name)
@Html.HiddenFor(m => m[i].first_name)
</td>
<td>
@Html.DisplayFor(m => m[i].last_name)
@Html.HiddenFor(m => m[i].last_name)
</td>
<td>
@Html.TextBoxFor(m => m[i].Cost1)
@Html.HiddenFor(m => m[i].Cost1)
</td>
<td>
@Html.TextBoxFor(m => m[i].Cost2)
@Html.HiddenFor(m => m[i].Cost2)
</td>
</tr>
}
}
</tbody>
ajax script
<script>
$("#export").click(function (event)
{
var object = @(Html.Raw(Json.Encode(Model)));
$.ajax({
type: 'POST',
url: '/Student/StudentData',
data: JSON.stringify({ modelData: object }),
contentType: 'application/json',
dataType: 'json',
success: function (response)
{
if (response != '')
console.log("Success");
},
failure: function (response)
{
console.log("Failure");
},
error: function (response)
{
console.log("Error:" + response);
}
});
});
</script>
In my controller, I am getting 0 for both Cost1 and Cost2, how do I pass the new value for each columns?
List<StudentDomainModel>I am getting null, this is my controller[HttpPost] public FileStreamResult StudentData(List<StudentDomainModel> modelData) { }data: { 'formResult': formJsonData },todata: JSON.stringify(formJsonData),but still getting null in my controller