0

I have a form in view called Role which contains form for filling values for Role. For this i have use model in view.My code is

@using MyProject.Models
@model Role
<h2>AddRole</h2>
@using (Html.BeginForm()){ 
@Html.LabelFor(m=>m.RoleName)
@Html.TextBoxFor(m=>m.RoleName)<br />

<input type="submit" value="Add Role" />
}

After submitting,it goes to AddRole action in my controller,where I am adding this role to database and return back to same view. code for AddRole is,

 [HttpPost]
 public ActionResult AddRole(Role role)
 {
 //Code for adding role into database
 using (UserDbContext db = new UserDbContext())
 ViewBag.Message= "Role successfully added.";
 return View("AddRole");
 }

After adding values to database,it is still showing the already type values in textboxes. I want it must not show those values after adding into database.

I think it is,sending that role values back to view,that's why it is showing those values in form.So i try to make that value null in action.But not work. Thanks in advance.

2 Answers 2

1

You could either do ModelState.Clear() or do a GET request back to your AddRole page i.e. return RedirectToAction("AddRole"). However, the latter would not keep keep your ViewBag.Message;

Sign up to request clarification or add additional context in comments.

Comments

0

Try using

Role newRole=new Role();
ModelState.Clear();  // the fix
return View("AddRole",newRole);

Update me if it Worked?

3 Comments

whats issue, is output same?
As @david stated try using ModelState.Clear()
yes it worked by using ModelState.Clear() and also by return RedirectToAction("AddRole") but it is not keeping message in ViewBag,for that I use tempData

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.