Reputation: 21
I am learning ASP.NET MVC. After the form submission, I want to display a success message with partial view under the submit button on main view.
<form action="myactionLInk" method="post">
<input type="text" name="Name" placeholder="Enter name here..."/>
<input type="submit" value="Submit" />
</form>
// Here would be some code to load partialview
<div>
@Html.Partial("_userpartial")
</div>
Here is my UserContoller
:
[HttpPost]
public ActionResult userData(FormCollection collection)
{
//Save name, email and contact to ViewBag
return PartialView("_userPartial")
}
Here is _userPartial.cshtml
:
<p> Data of @Viewbag.Name successfully saved.</p>
I want the partial view to be loaded under the submit button on successful creation. I am still learning MVC so any help is appreciated.
Upvotes: 2
Views: 7940
Reputation: 32059
Why do you need partial view to show this success message? you can do simply as follows:
As you said your very new to ASP.NET MVC, so here I am giving you a full solution to how you can show the success message after an entity creation.
First write a model class follows:
public class User
{
[Key]
public int UserId {get; set;}
[Required]
public int UserName {get; set;}
}
Then in the User
Controller:
public class UserController: Controller
{
[HttpGet]
public ActionResult CreateUser()
{
return View();
}
[HttpPost]
public ActionResult CreateUser(User user)
{
if(ModelState.IsValid)
{
// save the user here
ViewBag.SuccessMessage = user.UserName + " has been created successfully!"
ModelState.Clear();
return View();
}
return View(user);
}
}
Then in your CreateUser.cshmtl
view:
@using User
@using (Html.BeginForm("CreateUser", "User", FormMethod.Post))
{
@Html.EditorFor(m => m.UserName)
<input type="submit" value="Submit" />
}
@{
if(ViewBag.SuccessMessage != null)
{
<div>
@ViewBag.SuccessMessage
</div>
}
}
Hope it will help you.
Upvotes: 3