Reputation: 21
I am new to MVC and I am tring to create a 'Registration-Form' in MVC4.
I have created a 'Registration-Form' which adds data to database Table 'Registration' on click of submit.
I have a 'Web-Grid' on same page just below the 'Registration-Form' which displays the data present in database table 'Registration'.
Both operation works fine in different views. But when I am trying to implement in one view there is a problem.
1st operation need normal model variable.
2nd operation need IEnumerable model variable.
How can I resolve this problem.
Here is my code:
Controller:
namespace RegistrationMVC.Controllers
{
public class RegistrationController : Controller
{
//
// GET: /Registration/
public ActionResult Index(Registration model)
{
UserContext dbContext = new UserContext();
var details = dbContext.Registration.ToList();
return View(details);
}
public enum Command
{
Create,
Reset
}
[HttpPost]
public ActionResult Index(Registration model, Command command)
{
if (command == Command.Create)
{
if (ModelState.IsValid)
{
UserContext dbContext = new UserContext();
var newRegistration = new Registration() { Email = model.Email, Password = model.Password, FName = model.FName, LName = model.LName, Contact = model.Contact, Address = model.Address };
dbContext.Registration.Add(newRegistration);
dbContext.SaveChanges();
return RedirectToAction("Index");
}
}
else
{
return RedirectToAction("Index");
}
return View(model);
}
}
}
Index View:
@model IEnumerable<RegistrationMVC.Models.Registration>
@{
ViewBag.Title = "Index";
var grid = new WebGrid(Model);
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@Html.Partial("_NewRegistration")
@grid.GetHtml()
</body>
</html>
Registration Partial View:
@model RegistrationMVC.Models.Registration
<script src="~/Scripts/jquery-2.0.3.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "Registration Failed. Please correct the errors and try again.")
<div>
<fieldset>
<legend>Registration</legend>
@Html.LabelFor(model => model.Email, "Email:")
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
<br />
@Html.LabelFor(model => model.Password, "Password:")
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
<br />
@Html.LabelFor(model => model.FName, "First Name:")
@Html.EditorFor(model => model.FName)
@Html.ValidationMessageFor(model => model.FName)
<br />
@Html.LabelFor(model => model.LName, "Last Name:")
@Html.EditorFor(model => model.LName)
@Html.ValidationMessageFor(model => model.LName)
<br />
@Html.LabelFor(model => model.Contact, "Contact No:")
@Html.EditorFor(model => model.Contact)
@Html.ValidationMessageFor(model => model.Contact)
<br />
@Html.LabelFor(model => model.Address, "Address:")
@Html.TextAreaFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
<br />
<input type="submit" name="Command" value="Create" />
<input type="submit" name="Command" value="Reset" />
</fieldset>
</div>
}
Upvotes: 2
Views: 1475
Reputation: 21
I have Using partial Views and providing Model as needed to the specific partial view
Upvotes: 0