<div style="color: #000000; font: normal 12px verdana;">
I have an Index view that shows a list of employees and a Create partial view with a form and submit button to create employees. In the Index view, I have:
@{Html.RenderAction("Create");}
A user sees a blank form to create employees and a list of employees. After the user has typed data in the form and clicked the submit button, the form should become blank and the list of employees should be refreshed, showing employees plus the new employee.
The problem is I don't know what to return from the Create action:
public ActionResult Create(Employee employee)
{
return ???
}
Can this be accomplished without JavaScript? And how?
<div style="color: #000000; font: normal 12px verdana;">
@ignatandrei
You've broken my issue into two parts. I'd like to keep it simple.
The Index view "imports" the Create partial view. A user types in a new employee. The user hits the create button. In response to this activity, the user should see a blank form and the updated list of employees. Hence, from the user's point of view, the
entire page is refreshed.
The blank form confuses me. If I take" bank form " apart, yes, can be done without javascript.
1. Make a HttpGet action that takes the employees from database
2. In the view use a "submit" button
2. In the HttpPost action add the new employee to database and RedirectToAction ( - the httpget action)
<div style="color: #000000; font: normal 12px verdana;">
@ignatandrei
Aha. But then the application will work differently. The idea is to isolate the employee creation form in a partial view to be re-used in other parts of the website.
anita malmo
Member
38 Points
30 Posts
mvc 3 : view containing partial view with form
Mar 27, 2011 10:46 PM|LINK
I have an Index view that shows a list of employees and a Create partial view with a form and submit button to create employees. In the Index view, I have:
@{Html.RenderAction("Create");}A user sees a blank form to create employees and a list of employees. After the user has typed data in the form and clicked the submit button, the form should become blank and the list of employees should be refreshed, showing employees plus the new employee.
The problem is I don't know what to return from the Create action:
public ActionResult Create(Employee employee) { return ??? }Can this be accomplished without JavaScript? And how?
</div>Abdalmohayme...
Member
466 Points
89 Posts
Re: mvc 3 : view containing partial view with form
Mar 27, 2011 11:16 PM|LINK
I know asolution If you use Ajax
use UpdateTargetId = "div which countin render list" in ajax.beginform
If you don't use ajax I think you don't need to any think just use return RedirectToAction();
I hope to advice you
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: mvc 3 : view containing partial view with form
Mar 28, 2011 05:58 AM|LINK
this can be achieved ONLY with Javascript
if you want ONLY this list to be refreshed( and not the whole page) , this can be achieved only with JavaScript
Please see
http://msprogrammer.serviciipeweb.ro/2011/02/13/asp-net-mvc-jquery-and-razor-cascading-dropdown-retrieving-partial-views-json-send-objects-handling-errors/
anita malmo
Member
38 Points
30 Posts
Re: mvc 3 : view containing partial view with form
Mar 28, 2011 09:34 AM|LINK
@ignatandrei
You've broken my issue into two parts. I'd like to keep it simple.
The Index view "imports" the Create partial view. A user types in a new employee. The user hits the create button. In response to this activity, the user should see a blank form and the updated list of employees. Hence, from the user's point of view, the entire page is refreshed.
Can this be done without JavaScript?
</div>ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: mvc 3 : view containing partial view with form
Mar 28, 2011 10:26 AM|LINK
The blank form confuses me. If I take" bank form " apart, yes, can be done without javascript.
1. Make a HttpGet action that takes the employees from database
2. In the view use a "submit" button
2. In the HttpPost action add the new employee to database and RedirectToAction ( - the httpget action)
anita malmo
Member
38 Points
30 Posts
Re: mvc 3 : view containing partial view with form
Mar 28, 2011 02:23 PM|LINK
@ignatandrei
Ignore blank forms for the time being. What should the HttpPost Create action method return? In a nutshell, my code looks as follows:
EmployeeController
public class EmployeeController : Controller { public ActionResult Index() { . . . return View(employeeList); } public ActionResult Create() { return PartialView(); } [HttpPostAttribute] public ActionResult Create(Employee employee) { if ( ModelState.IsValid ) { this.objectContext.Employees.Add(project); this.objectContext.SaveChanges(); } return ???; } }Index.cshtml
. . . @{Html.RenderAction("Create");} . . . <table title="List of employees"> ... </table>Create.cshtml contains a form to create new employees and submit button.
If I use
return RedirectToAction("Index");I get an error saying "Child actions are not allowed to perform redirect actions."
</div>ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: mvc 3 : view containing partial view with form
Mar 28, 2011 02:26 PM|LINK
please modify :
1. modify your partial view employees such as does not contain a form
2. modify index and put Html.BeginForm around your Html.RenderAction
3. modify [HttpPOst] Create in [HttpPOst ]Index
4.return RedirectToAction("Index");
anita malmo
Member
38 Points
30 Posts
Re: mvc 3 : view containing partial view with form
Mar 28, 2011 02:43 PM|LINK
@ignatandrei
Aha. But then the application will work differently. The idea is to isolate the employee creation form in a partial view to be re-used in other parts of the website.
</div>tanatrajan
Participant
1784 Points
370 Posts
Re: mvc 3 : view containing partial view with form
Mar 28, 2011 04:18 PM|LINK
try something like this
return View(new Employee());
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: mvc 3 : view containing partial view with form
Mar 28, 2011 07:40 PM|LINK
In that case it should work if you rename the HttpPost action in somewhat different from the action returning the PartialView