Display success message on the same page when sending

I use Html.Beginform on the view page and get the parameters using FormCollection for the controller. I want to return a Success message in the same ViewPage as the result. I am using the following code,

 public string InsertDetails(FormCollection collection) { string result = "Record Inserted Successfully!"; return result; } 

It shows a success message on a new page. How can i solve this? What should I return to receive the message "Success" on the same page?

+6
source share
3 answers

Personally, I would insert the result string into the ViewBag.

 public ActionResult InsertDetails(FormCollection collection) { //DO LOGIC TO INSERT DETAILS ViewBag.result = "Record Inserted Successfully!"; return View(); } 

Then on the web page:

 <p>@ViewBag.result</p> 
+11
source

I have the following options.

1. Use the Ajax Begin Form with AjaxOptions as shown below

 @using (Ajax.BeginForm("ActionName", "ControllerName", new { area = "AreaName" }, new AjaxOptions { HttpMethod = "POST", OnSuccess = "alert('Success');" //This will execute once the Ajax call is finished. }, null)) { <input type="submit" name="nameSubmit" value="Submit" /> } 

2. Use jQuery to manually install the XHR query

 $.ajax({ url: "@Url.Action("ActionName", "ControllerName", new { area = "AreaName" });", type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify({param : Value}) }) .done(function () { alert('Success');}) //This will execute when you request is completed. .fail(function () { }) 

My suggestions

When using FormCollection

The following disadvantages exist:

Point - 1

In the case of using FormCollection ... Mandatory Type Cast value of Primitive Type not necessary, because when receiving a record of a specific index System.Collections.Specialized.NameValueCollection , the return value is of type String . This situation does not occur in the case of strongly typed View-Models .

Problem - 2

When you submit the form and go to the Post Action Method and View-Model as a parameter in the Action method, you have a condition to send the submitted View values ​​back. Otherwise, write the code again to send back via TempData/ViewData/ViewBag

enter image description here



Point - 3

We have data annotations that can be implemented in the View Model or Custom Validations .

enter image description here

ASP.Net MVC simplifies validation modeling using Data Annotation. Data annotations are attributes that apply to properties. We can create our own verification attribute by inheriting the built-in verification attribute class.



Point - 4

For example, you have the following HTML

 <input type="text" name="textBox1" value="harsha" customAttr1 = "MyValue" /> 

Question How can we access the value of customAttr1 from the above, for example, from inside the controller

Answer : when submitting the form, only the name and value of the elements are sent back to the server. You can also use hidden fields to publish the Attributes to Post Action method.

Alternatives . Use jQuery bit to get custom attribute values ​​and send them along with form values ​​to the action method

Another option is to rather place what you got in your user attributes in hidden controls.




For this reason, I would always prefer to use View-Models

+11
source

we can do this on a form inside a view

  @using (Ajax.BeginForm("Action", "Controller", new AjaxOptions { HttpMethod = "POST", OnSuccess = "Showmessage" })) [HttpPost] public ActionResult Test(TestViewModel model) { return Json(new {isok=true, message="Your Message" }); } function Showmessage(data) { $('#Element').html('Successfully Submitted'); } 
0
source

Source: https://habr.com/ru/post/1490874/


All Articles