StackOverflowException is caused by a default mvc view - c#

I created a simple create view for a model by mvc's automated way to show to a coleague.
Now, although i do not really use that, i can't see where a recursion is caused since i just said to mvc to create it for a model where all the fields are made with the
"public 'type' 'name' { get; set;}" format. I will include the model, action, and view below.
This isn't really a problem for me since i do not use the templates at all but I am curious how this could happen when no custom code was inserted
Thanks in advance
Controller Action
public ActionResult View()
{
return View();
}
Model
namespace Data_Access.Models
{
public class StudentModel
{
public int studentId { get; set; }
public string name { get; set; }
public string surname { get; set; }
public string classroom { get; set; }
public string role { get; set; }
public string imgPath { get; set; }
}
}
And finally the View
#model Data_Access.Models.StudentModel
#{
ViewBag.Title = "View";
}
<h2>View</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>StudentModel</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.studentId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.studentId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.studentId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.name, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.name, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.surname, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.surname, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.surname, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.classroom, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.classroom, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.classroom, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.role, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.role, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.role, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.imgPath, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.imgPath, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.imgPath, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>

The controller has infinite recursion:
public ActionResult View()
{
return View();
}
Your View() method calls the same View() method ;)
Probably naming problem - I can't suggest any solution here, because you didn't take any information about the flow, what should be implemented in controller.

You can change action name to ViewPage() and add route attribute Route[("View")] above the action.
example:
[Route("View")]
public IActionResult ViewPage()
{
return View();
}
then rename View.cshtml to ViewPage.cshtml
This will prevent recursive and you also use url path /View
I hope this will help you.

Related

Input type file not showing ASP.NET

I have this model:
public class PostAddRequest
{
public string Title { get; set; }
public string Description { get; set; }
public string Content { get; set; }
public int CategoryId { get; set; }
public string FeaturedImagePath { get; set; }
public HttpPostedFileBase FeaturedImage { get; set; }
public List<string> GalleryImagesPath { get; set; }
public HttpPostedFileBase[] GalleryImages { get; set; }
}
Controller:
[HttpGet]
public ActionResult Create()
{
return View(new PostAddRequest());
}
View:
#model BusinessObjects.Requests.PostAddRequest
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm("Create", "Post", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>PostAddRequest</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Title, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Title, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Title, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Description, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Description, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Content, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Content, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Content, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CategoryId, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CategoryId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CategoryId, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FeaturedImage, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.FeaturedImage, new { type = "file", htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FeaturedImage, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.GalleryImages, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.GalleryImages, "", new { #type = "file", #multiple = "multiple" })
#Html.ValidationMessageFor(model => model.GalleryImages, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
I'm getting this error: Compiler Error Message: CS1061: 'PostAddRequest' does not contain a definition for 'GalleryImages' and no accessible extension method 'GalleryImages' accepting a first argument of type 'PostAddRequest' could be found (are you missing a using directive or an assembly reference?)
I want to upload an image for FeaturedImage and multiple images for GalleryImages, but for some reason I'm getting the above error. Any idea on how to fix this?
you should use List<IFormFile> for multiple files.
public List<IFormFile> GalleryImagesPath { get; set; }

ASP.NET - Submitting a form does not pass data

I have a class like so:
public class saveModel
{
public int ID{ get; set; }
[DisplayName("Area")]
public string Area { get; set; }
public string AreaDescription { get; set; }
[DisplayName("Model")]
public string Model { get; set; }
[DisplayName("Description")]
}
and in my controller I have this:
[Authorize]
public ActionResult ModelEdit(int id)
{
saveModel model = webService.getModel(id);
return View(model);
}
[Authorize]
[HttpPost]
public ActionResult ModelEdit(saveModel model)
{
return View();
}
and in view I have this:
#model MyApp.Models.saveModel
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.ModelID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ModelID, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ModelID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Area, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Area, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Area, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.AreaDescription, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AreaDescription, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AreaDescription, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Model, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Model, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Model, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
The problem I am having is when I press the save button, it goes to the correct method, but my saveModel is empty on submit...what am I doing wrong?
try including the from body attribute as below
[Authorize]
[HttpPost]
public ActionResult ModelEdit([FromBody]saveModel model)
{
return View();
}
if it doesn't solve your problem, then an invalid or an empty value is being passed to your ID field. You can check the ModelState property, which will point to the right field.

C# MVC can't retrieve data from view to controller [duplicate]

This question already has answers here:
Asp.Net MVC: Why is my view passing NULL models back to my controller?
(2 answers)
Closed 4 years ago.
I am new to ASP.Net and I am sure this is a very basic question. I have a employee crud views . In create view i was created viewModel to pass different two model . One Model display emloyee and another model connect to db and retrieve all departments to my view . But I can't retrieve data to my employee create controller area. Vs send this err Abc.Models.MyViewModel.Employee.get returned null. On Debug.Print(employee.Employee.Name) line.
Here my Models ;
Employee.cs
public class Employee
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Surname { get; set; }
[Required]
public string phoneNumber { get; set; }
public string Detail { get; set; }
//Bu kısımda veri tabanı ilişkisi 1 to many olacak
public int DepartmentId { get; set; }
public Department Department { get; set; }
}
Department.cs
public class Department
{
public int Id { get; set; }
public string depName { get; set; }
public List<Employee> Employees { get; set; }
}
MyViewModel.cs
public class MyViewModel
{
public Employee Employee { get; set; }
public IEnumerable<Department> departments { get; set; }
}
I added dropboxlist on my create.cshtml area with MyViewModel and i can't retrieve all data on my controller .
Here my EmployeeController.cs ;
// GET: Employee/Create
[HttpGet]
public ActionResult Create()
{
MyViewModel viewModel = new MyViewModel();
Employee emp = new Employee();
viewModel.Employee = emp;
viewModel.departments = db.Departments;
return View(viewModel);
}
// POST: Employee/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(/*[Bind(Include = "Id,Name,Surname,phoneNumber,Department,Detail")]*/ MyViewModel employee)
{
//string emName = employee.Employee.Name;
Debug.Print(employee.Employee.Name);
if (ModelState.IsValid)
{
db.Employees.Add(employee.Employee);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
Here Create.cshtml "this html codes are in #using (Html.BeginForm()");
#model TelefonRehberi.Models.MyViewModel
<div class="form-horizontal">
<h4>Employee</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Employee.Name, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Employee.Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Employee.Name, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Employee.Surname, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Employee.Surname, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Employee.Surname, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Employee.phoneNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Employee.phoneNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Employee.phoneNumber, "", new { #class = "text-danger" })
</div>
</div>
<!--Dropdown List Olacak-->
<div class="form-group">
#Html.LabelFor(model => model.Employee.Department, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<!--Burada zorlandım umarım doğru kullanım olmuştur.-->
#Html.DropDownListFor(m => m.Employee.Department, new SelectList(Model.departments.Select(i => i.depName)), " - Select or Add -", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Employee.Department, "", new { #class = "text-danger" })
</div>
</div>
<!--Dropdown List Sonu Ayarlamalar Yapılacak.-->
<div class="form-group">
#Html.LabelFor(model => model.Employee.Detail, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Employee.Detail, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Employee.Detail, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
Add Form and try to do
#using (Html.BeginForm("ActionName","ControllerName", FormMethod.Post))
{
<div class="form-horizontal">
<!--add your other code here -->
<!--add your other code here -->
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
HTML Forms are required, when you want to collect some data from the site visitor. For example, during user registration you would like to collect information such as name, email address, credit card, etc.
A form will take input from the site visitor and then will post it to a back-end application such as CGI, ASP Script or PHP script etc. The back-end application will perform required processing on the passed data based on defined business logic inside the application.
There are various form elements available like text fields, textarea fields, drop-down menus, radio buttons, checkboxes, etc.
Ok, you were really close. In Create method you need to create Employee employee and not MyViewModel employee. So in controller just put this Create method:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Employee employee)
{
Debug.Print(employee.Name);
if (ModelState.IsValid)
{
//db.Employees.Add(employee);
//db.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
Then binding will work just fine and you will get your employee from the form.
Just in case bellow is also the code for the form in the Create View.
<h2>Create</h2>
#using (Html.BeginForm("Create", "Employee", FormMethod.Post))
{
<div class="form-horizontal">
<h4>Employee</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Employee.Name, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Employee.Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Employee.Name, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Employee.Surname, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Employee.Surname, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Employee.Surname, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Employee.phoneNumber, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Employee.phoneNumber, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Employee.phoneNumber, "", new { #class = "text-danger" })
</div>
</div>
<!--Dropdown List Olacak-->
<div class="form-group">
#Html.LabelFor(model => model.Employee.Department, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<!--Burada zorlandım umarım doğru kullanım olmuştur.-->
#Html.DropDownListFor(m => m.Employee.Department, new SelectList(Model.departments.Select(i => i.depName)), " - Select or Add -", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Employee.Department, "", new { #class = "text-danger" })
</div>
</div>
<!--Dropdown List Sonu Ayarlamalar Yapılacak.-->
<div class="form-group">
#Html.LabelFor(model => model.Employee.Detail, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Employee.Detail, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Employee.Detail, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}

Asp.net MVC DateTime error on valid dates

I'm working on a calendar where I can create events. While using the jquery datetime picker there is no live validation error, but when I press on create there is an validation error where it states that;
The value 'the date i filled in' is not valid for StartDateTime.
I think that after I press create it tries to validate on a different format. I don't know which one and how I check this.
Here you have the code that is related to creating this.
Event model:
public class Event
{
public int Id { get; set; }
public string Subject { get; set; }
public string Description { get; set; }
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
public string ThemeColor { get; set; }
public bool IsFullDay { get; set; }
}
The create page:
#model BudoschoolTonNeuhaus.Models.Event
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Admin_Layout.cshtml";
}
<div class="container">
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Event</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Subject, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Subject, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Subject, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Description, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Description, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.StartDateTime, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.StartDateTime, new { htmlAttributes = new { #class = "form-control datepicker" } })
#Html.ValidationMessageFor(model => model.StartDateTime, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.EndDateTime, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.EndDateTime, new { htmlAttributes = new { #class = "form-control datepicker" } })
#Html.ValidationMessageFor(model => model.EndDateTime, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ThemeColor, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ThemeColor, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ThemeColor, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.IsFullDay, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.IsFullDay)
#Html.ValidationMessageFor(model => model.IsFullDay, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "AdminIndex")
</div>
</div>
#section Scripts{
<script>
$('.datepicker').datepicker({ });
</script>
}
Thanks for your help in regard
// http://docs.jquery.com/Plugins/Validation/Methods/date
date: function( value, element ) {
return this.optional(element) || !/Invalid|NaN/.test(new Date(value).toString());}
Put a break point on line 1026 (?) of scripts/jquery.validate.js
I edited it to:
date: function (value, element) {
return this.optional(element) || moment(value, "DD-MMM-YYYY").isValid();
},
The other thing I did was to put DataAnnotations in the POCO class:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yyyy}")]
$("input[type='datetime']").datepicker({ dateFormat: "dd-M-yy" });
might work

How do I put the values of a model into a database table, from inside a controller?

I'm trying to learn ASP.net MVC. I'm using the pre-populated asp.net project that has a fake website within it to start. I have a form, and on submit, I want to put the values into a table in my database. I would also like to add functionality that if the email already exists in the database, I redirect them to another page.
I've added my own view, model, and controller.
Here's my databases (Side question: should I just put the table I created into DefaultConnection instead of Users.mdf?)
My Model:
public class RegisterLoyaltyViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
public string Phone { get; set; }
public string SecurityQuestion { get; set; }
public string SecurityAnswer { get; set; }
public string optSweepstakes { get; set; }
public string optEmails { get; set; }
}
My relevant controller code:
// POST: /Account/RegisterLoyalty
[HttpPost]
[AllowAnonymous]
public void RegisterLoyalty(RegisterLoyaltyViewModel model)
{
//Currently nothing here
}
I think what I need to do is hit the database inside the controller. I need to check if email already exists in the table, and if it does, redirect to page x. If email does not exist, simply submit the model to the database, and redirect to page y.
You need a data layer implementation to hit the database, there are various ways of doing it one of which is using Entity Framework, have a look at here to get some ideas, or alternatively you can use ADO.NET to connect to the database.
Create a controller action to GET the RegisterLoyalty view. That view contains a the RegisterLoyalty form. Have the form POST to the RegisterLoyalty action. Then you can perform your logic and add the model to the db if necessary.
//
// GET: /Account/RegisterLoyalty
[AllowAnonymous]
public ActionResult RegisterLoyalty()
{
return View();
}
//
// POST: /Account/RegisterLoyalty
[HttpPost]
[AllowAnonymous]
public ActionResult RegisterLoyalty(RegisterLoyaltyViewModel model)
{
var db = new AccountLoyaltyDbContext();
var emailExists = db.Loyalties.Any(x => x.EmailAddress == model.EmailAddress);
if (emailExists)
{
return RedirectToAction("X");
}
db.Loyalties.Add(model);
db.SaveChanges();
return RedirectToAction("Y");
}
//
// GET: /Account/X
[AllowAnonymous]
public ActionResult X()
{
return View();
}
//
// GET: /Account/Y
[AllowAnonymous]
public ActionResult Y()
{
return View();
}
RegisterLoyalty.cshtml
#model UserLoyalty.Models.RegisterLoyaltyViewModel
#{
ViewBag.Title = "RegisterLoyalty";
}
<h2>RegisterLoyalty</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>RegisterLoyaltyViewModel</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.EmailAddress, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.EmailAddress, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Password, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Password, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Password, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Phone, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Phone, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Phone, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SecurityQuestion, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SecurityQuestion, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SecurityQuestion, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SecurityAnswer, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SecurityAnswer, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SecurityAnswer, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.optSweepstakes, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.optSweepstakes, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.optSweepstakes, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.optEmails, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.optEmails, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.optEmails, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>

Categories