I checked more than one post in this site and tried more than one solution but still I cannot check the user exist or not when register new user , I tried the following code :
[HttpPost]
public ActionResult register(Registration reg)
{
if (ModelState.IsValid)
{
var userexist = db.Registration.Any(x => x.username == reg.username);
if (userexist)
{
ModelState.AddModelError("username", "User with this name already exists");
return View(reg);
}
else
{
db.Registration.Add(reg);
db.SaveChanges();
return RedirectToAction("Index");
}
}
return View();
}
this is registration model :
namespace registration.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Registration
{
public int Userid { get; set; }
[Required]
[Display(Name ="ID or Iqama No ")]
public string username { get; set; }
[Required]
[Display(Name = "Medical Record Number ")]
public int PatientNo { get; set; }
[Required]
[Display(Name = "Mobile ")]
public string Mobile { get; set; }
[Display(Name = "Email Address ")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
}
this is the view code and submit button there create button:
#model registration.Models.Registration
#{
ViewBag.Title = "Register New User";
}
<h2>register</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Registration Window</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.username, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10" >
#Html.EditorFor(model => model.username, new { htmlAttributes = new { #type = "number", #min = "0", #value = "0", #class = "form-control" } })
#Html.ValidationMessageFor(model => model.username, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PatientNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PatientNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PatientNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Mobile, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Mobile, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Email, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "", 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">
<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", "Login")
</div>
what is the missing in my code why its not working when click enter or tab or by mouse click its not checking ?
Your code correct and no errors ,
You said
"when click enter or tab or by mouse click its not checking ?"
This code will work when you click the button Submit or Create .
The code will validate the username exist or not and not when you navigate grom the username field.
Try this:
#using (Html.BeginForm("register", "YourControllerName", FormMethod.Post, new { #id = "LoginForm", #autocomplete = "off"}))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Registration Window</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.username, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10" >
#Html.EditorFor(model => model.username, new { htmlAttributes = new { #type = "number", #min = "0", #value = "0", #class = "form-control" } })
#Html.ValidationMessageFor(model => model.username, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PatientNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PatientNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PatientNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Mobile, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Mobile, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Email, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "", 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">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult register(Registration reg)
{
if (ModelState.IsValid)
{
var userexist = db.Registration.Any(x => x.username == reg.username);
if (userexist)
{
ModelState.AddModelError("username", "User with this name already exists");
return View(reg);
}
else
{
db.Registration.Add(reg);
db.SaveChanges();
return RedirectToAction("Index");
}
}
return View();
}
I have a model that I'm trying to edit with a form:
public class Basiclife
{
[Key]
public int Id { get; set; }
public int? ResponseId { get; set; }
public string Plantype { get; set; }
public int Enrolledftes { get; set; }
public decimal Pctemployer { get; set; }
public decimal Fixedbenamt { get; set; }
public decimal Salarymult { get; set; }
public decimal Bencap { get; set; }
}
And a view wrapper to edit it (with the editor in a separate partial view):
<h2>CreateBasicLifeResponse</h2>
<div id="planList">
#using (Html.BeginForm("CreateBasicLifeResponse", "Surveys"))
{
<div id="editorRows">
#foreach (var item in Model.basiclives)
{
#Html.Partial("_BasicLifeResponse", item)
}
</div>
#Html.ActionLink("Add", "BasicLifeResponse", null, new { id = "addItem", #class = "button" });
<input type="submit" value="submit" />
}
</div>
The wrapper's model is:
public class ResponseBasicLife
{
public Response response { get; set; }
public List<Basiclife> basiclives { get; set; }
}
Here's the partial view:
#using CustomSurveyTool.Models
#model Basiclife
<div class="editorRow">
#using (Html.BeginCollectionItem("basiclives"))
{
<div class="form-horizontal">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Plantype, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Plantype, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Plantype, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Enrolledftes, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Enrolledftes, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Enrolledftes, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Pctemployer, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Pctemployer, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Pctemployer, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Fixedbenamt, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Fixedbenamt, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Fixedbenamt, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Salarymult, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Salarymult, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Salarymult, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Bencap, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Bencap, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Bencap, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
X
</div>
</div>
}
</div>
Here's my controller action where I'm getting the proper responseId and assigning it to the form values:
public ActionResult CreateBasicLifeResponse(ResponseBasicLife model)
{
for (var i = 1; i < model.basiclives.Count; i++)
{
string currentUserId = User.Identity.GetUserId();
Response targetresponse = db.response.FirstOrDefault(x => x.Userid == currentUserId);
int responseid = targetresponse.Id;
model.basiclives[i].ResponseId = responseid;
db.basiclife.Add(model.basiclives[i]);
db.SaveChanges();
}
ResponseBasicLife basicliferesponse = new ResponseBasicLife
{
basiclives = new List<Basiclife>
{
new Basiclife { }
}
};
return View(basicliferesponse);
}
The only thing that's being written to the database is the ResponseID. How do I get the rest of the values to write to it?
This is a unique case where EditorFor could help. Essentially an object will have its editor template which is kind of like a partial view but specific to editing forms.
Secondly, the being form you are specifying is expecting the model of the view that you specified - in your case the viewWrapper. If for instance you specified a List<BasicLife>, then that is what the postBack shall be expecting, and not the BasicLife object. Below is how your postback would look like.
[HttpPost]
public ActionResult CreateBasicLifeResponse(List<BasicLife> model){
//your code goes here
}
From your view though, it looks clear that Model contains more than just a list. That is what shall be expected from the callback.
I am trying to write my first ASP.NET MVC application with Entity Framework (code first).
I am trying to insert data from form to specific table in database but it does not work. When I press "Submit" button, page is refreshing but table still has not any value. There is no error on page or in console.
Could you please take a look on code and help me ?
Below is my code:
View:
#model VeterinaryApp.Models.Clients
#{
ViewBag.Title = "AddClient";
Layout = "~/Views/SharedViews/_MainLayout.cshtml";
}
<h2>AddClient</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Clients</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<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.Email, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "", 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">
<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>
Controller:
public class AddClientController : Controller
{
// GET: AddClient
public ActionResult AddClient()
{
return View();
}
//POST
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ClientsId, Name, Surname, Email, Phone")] Clients clients)
{
if (ModelState.IsValid)
{
using (StoreContext db = new StoreContext()) //DbContext
{
db.Clients.Add(clients);
db.SaveChanges();
return RedirectToAction("Index");
}
}
return View(clients);
}
}
Model
public class Clients
{
public int ClientsId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public int Phone { get; set; }
public virtual ICollection<BookVisit> BookedVisits { get; set; }
public virtual ICollection<Animals> OwnedAnimals { get; set; }
}
I'm following this (https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/getting-started-with-mvc/getting-started-with-mvc-part7), but I am seeing some strange things I when I view the webpage.
1) The ReleaseDate says its a required filed (even though its not marked as such in the code) , and I cannot see why its doing this.
and
2) the Price "works" if the values is 100.50, or less . if its 100.51 or higher, then the message kicks in. My understanding is that the message should kick in # 100.01... or am I wrong ?
namespace Movies.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Movie
{
public int Id { get; set; }
[Required(ErrorMessage = "Titles are required")]
public string Title { get; set; }
public System.DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
[Required(ErrorMessage = "The Price is required.")]
[Range(5, 100, ErrorMessage = "Movies cost between £5 and £100.")]
public decimal Price { get; set; }
}
}
Could someone point out what I'm doing wrong ?
thanks
view code is
#model Movies.Models.Movie
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Movie</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.ReleaseDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ReleaseDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ReleaseDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Genre, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Genre, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Genre, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Price, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Price, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Price, "", 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>
First question.
Make your DateTime nullable like this:
public System.DateTime? ReleaseDate { get; set; }
Second question:
Specify Range number type to double with literal d like this:
[Range(5d, 100d, ErrorMessage = "Movies cost between £5 and £100.")]
public decimal Price { get; set; }
i m stuck with uploading 3pictures, please suggest how can i move ahead
if you want i can email or upload my full solution for you guys
below is few detail:
My Controller:
namespace TiresalesClaim.Controllers
{
public class TsiclaimsController : Controller
{
private TiresalesClaimContext db = new TiresalesClaimContext();
// GET: Tsiclaims
public ActionResult Index(string searchBy, string search, int? page)
{
// var claims = db.Tsiclaims.Include(d => d.disposition).ToList();
if (searchBy == "DistributorName")
{
return View(db.Tsiclaims.Where(x => x.DistributorName.Contains(search) || search == null)
.ToList().ToPagedList(page ?? 1, 20));
}
else
if (searchBy == "TyreSize")
{
return View(db.Tsiclaims.Where(x => x.TyreSize.Contains(search) || search == null)
.ToList().ToPagedList(page ?? 1, 20));
}
else
if (searchBy == "TyreBrand")
{
return View(db.Tsiclaims.Where(x => x.TyreBrand.Contains(search) || search == null)
.ToList().ToPagedList(page ?? 1, 20));
}
else
if (searchBy == "Disposition")
{
return View(db.Tsiclaims.Where(x => x.DispositionAsPerTSI.Contains(search) || search == null)
.ToList().ToPagedList(page ?? 1, 20));
}
else
{
return View(db.Tsiclaims.ToList().ToPagedList(page ?? 1, 20));
}
}
public ActionResult OrderByDistributorName(int? page)
{
var claim = from c in db.Tsiclaims
orderby c.DistributorName ascending
select c;
return View((claim).ToPagedList(page ?? 1, 20));
}
public ActionResult OrderBySize(int? page)
{
var claim = from c in db.Tsiclaims
orderby c.TyreSize ascending
select c;
return View((claim).ToPagedList(page ?? 1, 20));
}
public ActionResult OrderByBrand(int? page)
{
var claim = from c in db.Tsiclaims
orderby c.TyreBrand ascending
select c;
return View((claim).ToPagedList(page ?? 1, 20));
}
public ActionResult OrderByDisposition(int? page)
{
var claim = from c in db.Tsiclaims
orderby c.DispositionAsPerTSI descending
select c;
return View((claim).ToPagedList(page ?? 1, 20));
}
public ActionResult OrderByDate(int? page)
{
var claim = from c in db.Tsiclaims
orderby c.ClaimRecDate descending
select c;
return View((claim).ToPagedList(page ?? 1, 20));
}
// GET: Tsiclaims/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Tsiclaim tsiclaim = db.Tsiclaims.Find(id);
if (tsiclaim == null)
{
return HttpNotFound();
}
return View(tsiclaim);
}
// GET: Tsiclaims/Create
public ActionResult Create()
{
TableDbContext tdb = new TableDbContext();
ViewBag.distributor = new SelectList(tdb.DistributorNames, "Name", "Name");
ViewBag.size = new SelectList(tdb.TyreSizes, "Name", "Name");
ViewBag.brand = new SelectList(tdb.TyreBrands, "Name", "Name");
ViewBag.Dispo = new SelectList(tdb.DispositionAsPerTSIs, "Name", "Name");
return View();
}
// POST: Tsiclaims/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,DistributorName,ClaimRecDate,State,Country,TelNo,FaxNo,Email,TyreSize,TyreBrand,TyreSerialNo,RemainingTreadDept,TreadWear,DefectAsPerDistributor,TypeofTyreWear,DefectAsPerTSI,DispositionAsPerTSI,ClaimValueAwarded,InspectedBy,Picture1,Picture2,Picture3")] Tsiclaim tsiclaim)
{
if (ModelState.IsValid)
{
db.Tsiclaims.Add(tsiclaim);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tsiclaim);
}
// GET: Tsiclaims/Edit/5
public ActionResult Edit(int? id)
{
TableDbContext tdb = new TableDbContext();
ViewBag.distributor = new SelectList(tdb.DistributorNames, "Name", "Name");
ViewBag.size = new SelectList(tdb.TyreSizes, "Name", "Name");
ViewBag.brand = new SelectList(tdb.TyreBrands, "Name", "Name");
ViewBag.Dispo = new SelectList(tdb.DispositionAsPerTSIs, "Name", "Name");
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Tsiclaim tsiclaim = db.Tsiclaims.Find(id);
if (tsiclaim == null)
{
return HttpNotFound();
}
return View(tsiclaim);
}
// POST: Tsiclaims/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,DistributorName,ClaimRecDate,State,Country,TelNo,FaxNo,Email,TyreSize,TyreBrand,TyreSerialNo,RemainingTreadDept,TreadWear,DefectAsPerDistributor,TypeofTyreWear,DefectAsPerTSI,DispositionAsPerTSI,ClaimValueAwarded,InspectedBy,Picture1,Picture2,Picture3")] Tsiclaim tsiclaim)
{
if (ModelState.IsValid)
{
db.Entry(tsiclaim).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tsiclaim);
}
// GET: Tsiclaims/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Tsiclaim tsiclaim = db.Tsiclaims.Find(id);
if (tsiclaim == null)
{
return HttpNotFound();
}
return View(tsiclaim);
}
// POST: Tsiclaims/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Tsiclaim tsiclaim = db.Tsiclaims.Find(id);
db.Tsiclaims.Remove(tsiclaim);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
My Create Page:
#model TiresalesClaim.Models.Tsiclaim
#
{
ViewBag.Title = "Create";
}
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#*<h1>Tire Sales International</h1>*#
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#*<div class="form-group">
#Html.LabelFor(model => model.Id, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DisplayFor(model => model.Id, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Id, "", new { #class = "text-danger" })
</div>
</div>*#
<div class="form-group">
#Html.LabelFor(model => model.DistributorName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.DistributorName, (SelectList)ViewBag.distributor, "Select Distributor")
#*#Html.DropDownList("DistributorName", new List<SelectListItem> {
new SelectListItem{Text="Al Alaly Tyre", Value="Al Alaly Tyre"},
new SelectListItem{Text="Jilphar", Value="Jilphar"}
}, "Select Distributor Name")*#
#*#Html.EditorFor(model => model.DistributorName, new { htmlAttributes = new { #class = "form-control" } })*#
#Html.ValidationMessageFor(model => model.DistributorName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ClaimRecDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ClaimRecDate, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ClaimRecDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.State, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.State, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.State, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Country, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Country, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Country, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.TelNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.TelNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TelNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FaxNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FaxNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FaxNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Email, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.TyreSize, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.TyreSize, (SelectList)ViewBag.size, "Select Tyre Size")
#*#Html.DropDownList("TyreSize", new List<SelectListItem> {
new SelectListItem{Text="GD 1200r24 20pr HF702", Value="GD 1200r24 20pr HF702"},
new SelectListItem{Text="GD 1200r24 20pr GD122", Value="GD 1200r24 20pr GD122"},
new SelectListItem{Text="TF 1200r24 20pr TF801", Value="TF 1200r24 20pr TF801"},
new SelectListItem{Text="TF 1200r24 20pr TF580", Value="TF 1200r24 20pr TF580"},
new SelectListItem{Text="YG 1200r24 20pr FH326", Value="YG 1200r24 20pr FH326"},
new SelectListItem{Text="GD 315/80r22.5 18pr GD660", Value="GD 315/80r22.5 18pr GD660"},
new SelectListItem{Text="GD 315/80r22.5 18pr GD768", Value="GD 315/80r22.5 18pr GD768"},
new SelectListItem{Text="YG 315/80r22.5 18pr FH158", Value="YG 315/80r22.5 18pr FH158"},
new SelectListItem{Text="YG 315/80r22.5 18pr FH159", Value="YG 315/80r22.5 18pr FH159"},
new SelectListItem{Text="GD 385/65r22.5 20pr GD022", Value="GD 385/65r22.5 20pr GD022"},
new SelectListItem{Text="YG 385/65r22.5 20pr FH538", Value="YG 385/65r22.5 20pr FH538"},
}, "Select Tyre Size")*#
#*#Html.EditorFor(model => model.TyreSize, new { htmlAttributes = new { #class = "form-control" } })*#
#Html.ValidationMessageFor(model => model.TyreSize, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.TyreBrand, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.TyreBrand, (SelectList)ViewBag.brand, "Select Tyre Brand")
#*#Html.DropDownList("TyreBrand", new List<SelectListItem> {
new SelectListItem{Text="GreenDragon Brand Tyre", Value="GreenDragon Brand Tyre"},
new SelectListItem{Text="Tuffstone Brand Tyre", Value="Tuffstone Brand Tyre"},
new SelectListItem{Text="Young Brand Tyre", Value="Young Brand Tyre"}
}, "Select Tyre Brand")*#
#*#Html.EditorFor(model => model.TyreBrand, new { htmlAttributes = new { #class = "form-control" } })*#
#Html.ValidationMessageFor(model => model.TyreBrand, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.TyreSerialNo, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.TyreSerialNo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TyreSerialNo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.RemainingTreadDept, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.RemainingTreadDept, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.RemainingTreadDept, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.TreadWear, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.TreadWear, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TreadWear, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DefectAsPerDistributor, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DefectAsPerDistributor, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.DefectAsPerDistributor, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.TypeofTyreWear, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.TypeofTyreWear, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TypeofTyreWear, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DefectAsPerTSI, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DefectAsPerTSI, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.DefectAsPerTSI, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DispositionAsPerTSI, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model=>model.DispositionAsPerTSI,(SelectList)ViewBag.Dispo, "Select Disposition")
#*#Html.DropDownList("DispositionAsPerTSI", new List<SelectListItem> {
new SelectListItem{Text="Pass", Value="Pass"},
new SelectListItem{Text="Fail", Value="Fail"}
}, "Select Disposition As Per TSI")*#
#*#Html.EditorFor(model => model.DispositionAsPerTSI, new { htmlAttributes = new { #class = "form-control" } })*#
#Html.ValidationMessageFor(model => model.DispositionAsPerTSI, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ClaimValueAwarded, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ClaimValueAwarded, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ClaimValueAwarded, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.InspectedBy, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.InspectedBy, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.InspectedBy, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Picture1, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Picture1, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Picture1, "", new { #class = "text-danger" })
<input type="file" class="form-control" id="Picture1"/>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Picture2, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Picture2, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Picture2, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Picture3, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Picture3, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Picture3, "", 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" />*#
<input type="image" src="~/Icons/Save.png" alt="Submit" width="40" height="40">
</div>
</div>
</div>}<div><img src="#Url.Content("~/Icons/go-back-icon.png")" width="50" />Back</div>#section Scripts {#Scripts.Render("~/bundles/jqueryval")}
My Model:
namespace TiresalesClaim.Models
{
public class Tsiclaim
{
[Key]
[DisplayName("Claim Ref#")]
public int Id { get; set; }
//[Key]
//[Range(1,999999)]
//public int Id { get; set; }
[Required]
[DisplayName("Distributor Name")]
public string DistributorName { get; set; }
[DataType(DataType.Date)]
[DisplayName("Claim Rec Date")]
public DateTime ClaimRecDate { get; set; }
[Required]
[DisplayName("State")]
public string State { get; set; }
[Required]
[DisplayName("Country")]
public string Country { get; set; }
[DataType(DataType.PhoneNumber)]
[DisplayName("Tel #")]
public string TelNo { get; set; }
[DisplayName("Fax #")]
[DataType(DataType.PhoneNumber)]
public string FaxNo { get; set; }
[DisplayName("Email")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[DisplayName("Tyre Size")]
public string TyreSize { get; set; }
[Required]
[DisplayName("Tyre Brand")]
public string TyreBrand { get; set; }
//[Required]
//public string PlyRating { get; set; }
[Required]
[DisplayName("Tyre Serial #")]
public string TyreSerialNo { get; set; }
[Required]
[DisplayName("Rmaining Tread Dept")]
public decimal RemainingTreadDept { get; set; }
[Required]
[DisplayName("% Tread Wear")]
public decimal TreadWear { get; set; }
[Required]
[DisplayName("Defect As Per Distributor")]
public string DefectAsPerDistributor { get; set; }
[Required]
[DisplayName("Type of Tyre Wear")]
public string TypeofTyreWear { get; set; }
[Required]
[DisplayName("Defect As Per TSI")]
public string DefectAsPerTSI { get; set; }
[Required]
[DisplayName("Disposition As Per TSI")]
public string DispositionAsPerTSI { get; set; }
[Required]
[DisplayName("% Claim Value Awarded")]
public decimal ClaimValueAwarded { get; set; }
[Required]
[DisplayName("Inspected By")]
public string InspectedBy { get; set; }
[DataType(DataType.Upload)]
public string Picture1 { get; set; }
[DataType(DataType.Upload)]
public string Picture2 { get; set; }
[DataType(DataType.Upload)]
public string Picture3 { get; set; }
[DisplayName("Status")]
public string Status { get; set; }
[DisplayName("Remarks")]
public string Remarks { get; set; }
//[NotMapped]
//public virtual DispositionAsPerTSI disposition { get; set; }
}
}
You need to get the file data from object containing the file data, i.e. HttpPostedFileBase. You can then get the data and the fully qualified name/path and save them separately as needed.
See this link for an example: Uploading a File (Or Files) With ASP.NET MVC
Here is a quick example:
VIEW:
<form action="" method="post" enctype="multipart/form-data">
<label for="file1">Filename:</label>
<input type="file" name="files" id="file1" />
<label for="file2">Filename:</label>
<input type="file" name="files" id="file2" />
<input type="submit" />
</form>
CONTROLLER:
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}