0

I have to send data from view to Action so that I could access it as Model

My scripts:

        loadData(Element);
        var id = $(this).parent().closest('div').attr('id'));
        $.ajax({
            url: "/FormsCreation/EditLoadForm/"+ id +"?otherparameters...",
            type: 'GET',
            dataType: "json",
            data: JSON.stringify(data),
            success: function (result) {
                $('#modalContent').load(result);
                $('#modalDiv div[class="modal-header"]').find('h3').text('Edit DataEntry');
                $('#modalFooter').attr('class', 'modal-footer hide');
                $.validator.unobtrusive.parse($("#modalContent form"));
                $('#modalDiv').modal({
                    backdrop: 'static',
                    keyboard: true
                }, 'show');
                loadDDL();
                loadCheckBox();
                EditbindForm(this);
            }
        });

    function loadData(edit) {
        var tds = edit.find('td');
        var type = new Array();
        var fieldid = new Array();
        var val = new Array();
        for (var i = 0; i < tds.length - 1; i++) {
            var chldrn = tds.eq(i).children('input');
            for (var j = 0; j < chldrn.length; j++) {
                type.push(chldrn.find('[name*="field_type"]').eq(j).val());
                fieldid.push(chldrn.find('[name*="field_id"]').eq(j).val());
                val.push(chldrn.find('[name*="value"]').eq(j).val());
            }
        }
        var fielddtls = {
            field_id: fieldid,
            field_type: type,
            value: val
        };
        var data = {
            formfields: fielddtls
        };
    }

I get Values from required fields and fill it into "data" and i wish to pass it to the 'GET' Action in Controller as below:

    public ActionResult EditLoadForm(int formid, int id, string type, string ImageName = "None", FormsCreationModel data = null)
    {
        ViewBag.SubFormId = id;
        var moditem = new FormsCreationModel();
        if (type == "Form")
        {
            moditem = formRepository.GetFormValues(formid, id);
        }
        else
            moditem = formRepository.GetTempValues(formid, ImageName);
        moditem.type = type;
        return View(moditem);
    }

My Model

public class FormsCreationModel
{
    public int form_id { get; set; }
    [Display(Name="Form Name")]
    public string form_name { get; set; }
    public string type { get; set; }
    [Display(Name="Batch No")]
    public int BatchId { get; set; }
    public string ImageName { get; set; }
    public string UserId { get; set; }
    public int SubId { get; set; }
    public IList<ListFormDataModel> ListData {get;set;}
    public IList<FormDetailsModel> formfields { get; set; }
    public string loadType { get; set; }
}

Some Referred Links:

How to send data back to controller in MVC using knockout

How to pass multiple parameters in json format to a web service using jquery?

Post an Array of Objects via JSON to ASP.Net MVC3

Question

My Question is that whether i am doing it right? Right now I am getting "Internal Server Error 500". I want to make sure that I am in the right direction and also to know any other possibilities such using Knockout. Please guide me on this. I am Newbie towards both MVC 4 and javascripts, so need a little detailed answer rather than precise one..

Community
  • 1
  • 1
parkourkarthik
  • 834
  • 1
  • 8
  • 20
  • Make sure first that the object you want to pass on server side has same name as provided in your action method signature. If you have argument with name "Form_ID" in action method then your passed json object should ahve same property name. In case you are accepting model object as an argument in your action method, you should pass correct property name in that case too. – K D Jun 16 '14 at 14:33
  • First off, you should really get in the habit of some normal naming conventions - especially when you have to incorporate weakly-typed parts like AJAX. In that model alone you have four styles! CamelCase, pascalCase, under_score, and lowercase .. with `ListName`, `loadType`, `form_name`, and `formfields`. The convention is CamelCase for model properties – arserbin3 Jun 16 '14 at 14:33
  • @K D So am i passing the argument properly. In my case the json object 'data' to suite the optional Model 'data' in my action?!! I hope covered the same for the inner arguments too. I think this is the point you are talking about.. – parkourkarthik Jun 17 '14 at 03:55
  • @arserbin3 thanks for the proper convention details. It would be better if you could give me a link which has complete convention details for all types of codes. – parkourkarthik Jun 17 '14 at 03:58

0 Answers0