Answered by:
Adding Record with No Values

Question
-
User1979860870 posted
Hi
https://imgur.com/undefined
[HttpPost] [ActionName("Create")] public ActionResult Create_Post(Location objLocation) { try { //TryUpdateModel(objEmployee); //var allErrors = ModelState.Values.SelectMany(v => v.Errors); if (ModelState.IsValid) { objDal.AddLocation(objLocation); return new JsonResult(objLocation); } else { var allErrors = ModelState.Values.SelectMany(v => v.Errors.Select(b => b.ErrorMessage)); } } catch (System.Data.DataException) { ModelState.AddModelError("", "Unable to Save Changes. Try Again, and if the problem persists contact System Administrator."); } return RedirectToAction("Index"); } ****************************************** function Add() { var objLocation = { Name: $('#Name').val(), Street: $('#Street').val(), City: $('#City').val(), }; $.ajax({ url: "/Location/Create", data: JSON.stringify(objLocation), type: "POST", contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { loadData(); $('#myModal').modal('hide'); }, error: function (errormessage) { alert(errormessage.responseText); } }); } ******************************************** <div class="container"> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" onclick="clearTextBox();">Add New Location</button><br /><br /> <table id="example" class="table table-bordered table-hover"> <thead> <tr> <th> Code </th> <th> Name </th> <th> Street </th> <th> City </th> <th> State </th> <th> Zip </th> <th> Country </th> <th> PanNo </th> <th> GstRegNo </th> <th> Mobile </th> <th> Phone-1 </th> <th> Phone-2 </th> <th> EMail </th> <th> Action </th> </tr> </thead> <tbody class="tbody"> </tbody> </table> </div> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title" id="myModalLabel">Add Employee</h4> </div> <div class="modal-body"> <form> <div class="form-group"> <label for="Code">Code</label> <input type="text" class="form-control" id="Code" placeholder="Code" /> </div> <div class="form-group"> <label for="Name">Name</label> <input type="text" class="form-control" id="Name" placeholder="Name" /> </div> <div class="form-group"> <label for="Street">Street</label> <input type="text" class="form-control" id="Street" placeholder="Age" /> </div> <div class="form-group"> <label for="City">City</label> <input type="text" class="form-control" id="City" placeholder="State" /> </div> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary" id="btnAdd" onclick="return Add();">Add</button> <button type="button" class="btn btn-primary" id="btnUpdate" style="display:none;" onclick="Update();">Update</button> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div>
Thanks
Wednesday, January 20, 2021 3:44 PM
Answers
-
User1312693872 posted
Hi,jagjit saini
Do you mean when you click 'Add', but the Create action can not get the model value?
You should add [Frombody], like the following, it specifies that a parameter or property should be bound using the request body.
public ActionResult Create_Post([FromBody] Location objLocation)
Best Regards,
Jerry Cai
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 21, 2021 2:38 AM
All replies
-
User-474980206 posted
you don't specify, what is happening, but your ajax action is returning json on success else html. it should return json or an error:
[HttpPost] [ActionName("Create")] public ActionResult Create_Post(Location objLocation) { if (ModelState.IsValid) { objDal.AddLocation(objLocation); return new JsonResult(objLocation); } else { throw new Exception("bad data"); } }
Wednesday, January 20, 2021 4:13 PM -
User1312693872 posted
Hi,jagjit saini
Do you mean when you click 'Add', but the Create action can not get the model value?
You should add [Frombody], like the following, it specifies that a parameter or property should be bound using the request body.
public ActionResult Create_Post([FromBody] Location objLocation)
Best Regards,
Jerry Cai
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 21, 2021 2:38 AM