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.
Related
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; }
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.
Please help : the validator does not validate a double value:
When I type a Double value with a dot or a vergule the validator displays me an error message that the value is not valid:
when I type a value with vergule he gives me this message:
The LONGITUDE field must be a number.
while when I type a value with point he gives me this message:
The value "230.5" is not valid for ATTITUDE.
this is the code of the view:
#model Miam.Models.diverse_info_commerce
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>diverse_info_commerce</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.ID_COMMERCE, "ID_COMMERCE", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("ID_COMMERCE", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.ID_COMMERCE, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.VILLE, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.VILLE, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.VILLE, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.RUE, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.RUE, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.RUE, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.NUMERO, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.NUMERO, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.NUMERO, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LONGITUDE, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LONGITUDE, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LONGITUDE, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ATTITUDE, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ATTITUDE, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ATTITUDE, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PAYS, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PAYS, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PAYS, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.HEUR_OUVERTURE, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.HEUR_OUVERTURE, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.HEUR_OUVERTURE, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.HEUR_FERMETURE, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.HEUR_FERMETURE, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.HEUR_FERMETURE, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group" hidden>
#Html.LabelFor(model => model.CREATED_AT, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CREATED_AT, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.CREATED_AT, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group" hidden>
#Html.LabelFor(model => model.UPDATED_AT, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UPDATED_AT, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.UPDATED_AT, "", 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")
}
The model:
namespace Miam.Models
{
using System;
using System.Collections.Generic;
public partial class diverse_info_commerce
{
public int ID_POSITION { get; set; }
public Nullable<int> ID_COMMERCE { get; set; }
public string VILLE { get; set; }
public string RUE { get; set; }
public int NUMERO { get; set; }
public double LONGITUDE { get; set; }
public double ATTITUDE { get; set; }
public string PAYS { get; set; }
public System.DateTime HEUR_OUVERTURE { get; set; }
public System.DateTime HEUR_FERMETURE { get; set; }
public System.DateTime CREATED_AT { get; set; }
public System.DateTime UPDATED_AT { get; set; }
public virtual commerce commerce { get; set; }
}
}
The controller:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "ID_POSITION,ID_COMMERCE,VILLE,RUE,NUMERO,LONGITUDE,ATTITUDE,PAYS,HEUR_OUVERTURE,HEUR_FERMETURE")] diverse_info_commerce diverse_info_commerce)
{
if (ModelState.IsValid)
{
diverse_info_commerce.UPDATED_AT = DateTime.Now;
diverse_info_commerce.CREATED_AT= DateTime.Now;
db.diverse_info_commerce.Add(diverse_info_commerce);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.ID_COMMERCE = new SelectList(db.commerces, "ID_COMMERCE", "TITRE_COMMERCE", diverse_info_commerce.ID_COMMERCE);
return View(diverse_info_commerce);
}
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>
}
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>