Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

mvc json format yes or no

I have mvc / razor app using ajax to make some transactions. everything is working fine.
But just we will add more parameters in data and It is going to super long list. so I want to find better way to handle this codes below.
I am thinking using json format passing value from front to the back. but do not know how to do it.

my question is:

1. is it best practice to use json format to pass list of value back and forth? If yes, can you show me how to do it with my codes below?
2. If json is not, can you suggest me what to do?
3. On the end - code behind. I do have lead class. and hope the best will be JsonResult CreateLead(Domain.Lead l) so I do not have to worry too much about list too long.


Thanks


$.ajax({
                                url: "/Home/CreateLead",
                                datatype: "text",
                                data:
                                {
                                    'firstname': document.getElementById("first-name").value,
                                    'lastname': document.getElementById("last-name").value,
                                    'phonenumber': document.getElementById("phone-number").value,
                                    'emailaddress': document.getElementById("email-address").value,
                                    'totalcountadult': document.getElementById("total-count-adult").value,
                                    'totalcountkid': document.getElementById("total-count-kid").value,
                                    'pickupdate': document.getElementById("pickup-date").value,
                                    'dropoffdate': document.getElementById("dropoff-date").value,
                                    'fleetid': '@Request.Url.Segments[3]',
                                    'eventname': '@Request.QueryString["eventName"]',
                                    'currentcitystate': document.getElementById("current-city-state").value
                                },


[HttpPost]     
        public JsonResult CreateLead
            (
             string firstname, string lastname, string phonenumber,string emailaddress,string totalcountadult,
             string totalcountkid, string pickupdate, string dropoffdate,string fleetid, string eventname, string currentcitystate
	    
            )
{
return Json(l, JsonRequestBehavior.AllowGet);
}
  

Open in new window

Avatar of ITsolutionWizard
ITsolutionWizard
Flag of United States of America image

ASKER

any helps?
Avatar of Julian Hansen
1. Yes JSON is most definitely a good practice and is becoming the standard means of exchanging data between client and service. XML is still an option but it is too "heavy" and verbose - JSON is much more lightweight.

2. You should take a look at this API to do the conversion to JSON https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.json?view=aspnet-mvc-5.2
thanks. can you show me example in codes?
I don't know how to pass an object
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I mean on the code behind side. just use your previous example will work to me.
I am afraid I can't help you with the MVC part of the equation. There are many samples out there of how to do this.
it is ok. we just wait for other experts to help me
appreciate your time
it is ok. we just wait for other experts to help me
appreciate your time
The only reason I picked up this question was that it had no responses and I wanted to try and give you some assistence.

My recommendation is that you report this question and ask a Mod to send out a request to other experts in the .Net / MVC space to help you with the final bit.