Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

JavaScript
<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:

C#
[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);

        }
Posted
Updated 12-Sep-13 1:15am
v3

Post your data using Ajax request like
JavaScript
<pre lang="Javascript">
  $.ajax({
            url: "@Url.Action("Check", "Home")",
            type: "POST",
            data: {
                Name: $('.class1').html(),
                Email: $('.class2').html(),
                Mobile: $('.class3').html()
            },
            error: function () { alert('Error'); },
            successs: function (data) { alert('successs'); }
        });


MVC use JsonModelBinder to bind your model.No need to use Json.stringfy({jsonMyModel: {}})
 
Share this answer
 
Comments
Member 8454063 12-Sep-13 10:33am    
no it dose not work . i changed all code . put just one field . alert(xhr.status); is executed and shows 0 . so i changed code to below. now, althoghe dose not show Error and 0, but wil pass username null to controller like before .

function saveMyModel() {
var url = '@Url.Action("Save", "Home")';
$.post(url, {
username: ‘jack’
}, function (data) {
if (data)
alert("ok");
else
alert("Error ");

});
}
try to pass Ajax data as jQuery('#FormId').serialize()
 
Share this answer
 
Comments
Member 8454063 12-Sep-13 7:16am    
Pooja Dhanani thanks , is it possible you see my updated question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900