If you get JSON, you can do it differently.
You could wrap the code you posted in a JSON Result action. The following is a very simplified example:
[HttpPost] public JsonResult GetIncidentId(int customerId, string incidentNumber) { JsonResult jsonResult = null; Incident incident = null; try { incident = dal.GetIncident(customerId, incidentNumber); if (incident != null) jsonResult = Json(new { id = incident.Id }); else jsonResult = Json(new { id = -1 }); } catch (Exception exception) { exception.Log(); } return jsonResult; }
Calling through Javascript from the view and manually filling out the form (meh).
Or more elegantly, you can create an MVC model to store the received data and serialize JSON in this model. Example below:
From: http://www.newtonsoft.com/json/help/html/deserializeobject.htm
public class Account { public string Email { get; set; } public bool Active { get; set; } public DateTime CreatedDate { get; set; } public IList<string> Roles { get; set; } } string json = @"{ 'Email': ' james@example.com ', 'Active': true, 'CreatedDate': '2013-01-20T00:00:00Z', 'Roles': [ 'User', 'Admin' ] }"; Account account = JsonConvert.DeserializeObject<Account>(json);
Hope this helps and good luck with your application!
source share