hello
why my model will pass null?
username , Tell,Mobil ,... will not fill in controller .they are my model's fields(Hiddenfor).
when i make alert in script block , i see them correctly.
but will not pass to controller.
i think ViewModelX in controller is diffrent to jsonMyModel in script block.
how can i make a relation between them?
<script type="text/javascript">
$("#submit-btn2").click(function () {saveMyModel(); });
function saveMyModel() {
var e = document.getElementById("id_Send");
var str = e.options[e.selectedIndex].value;
var e2 = document.getElementById("id_Receive");
var str2 = e.options[e.selectedIndex].value;
alert(str);
alert(str2);
alert($(".tdRecievername").html());
alert($(".tdMobile ").html());
$.ajax({
url: '@Url.Action("Save", "Home")',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
jsonMyModel: {
username: $(".tdBuyername").html(),
Tell: $(".tdPhone").html(),
Name: $(".tdRecievername").html(),
Mobil: $(".tdMobile").html(),
Lname: $(".tdLname").html(),
ShFactor: $("#Sh-Factor").html(),
Address: $(".tdAddress").html(),
Cod: $(".tdPobox").html(),
id_send: str,
id_Receive:str2,
},
success: function (data) {
alert('im back');
alert(data);
},
error: function (xhr, ajaxOptions, error) {
alert(xhr.status);
alert('Error: ' + xhr.responseText);
}
}),
});
}
</script>
Controller:
[HttpPost]
public ActionResult save(ViewModelX jsonMyModel)
{
var data = true ;
if (ModelState.IsValid)
{
result = HelperClass.Insert(jsonMyModel.Address, jsonMyModel.Cod,
jsonMyModel.ShFactor, jsonMyModel. id_send,
jsonMyModel.idNoePardakht_sefaresh, jsonMyModel.id_Receive, jsonMyModel.Lname, jsonMyModel.Mobil, jsonMyModel.Name, jsonMyModel.Tell, jsonMyModel.username);
}
else
{
data = false;
}
return Json(data);
}