IDENTITY_INSERT is set to OFF in MVC application - sql

I am trying to create a new employee in MVC 5. I used CRUD to generate views and models. I edited a few lines of code and I get this error:
Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF.
Where I got this:
public ActionResult Create([Bind(Include = "Imie,Nazwisko,Pesel,Data_zatrudnienia,Data_zwolnienia,Pensja,Wyksztalcenie")] Osoba osoba,Pracownik pracownik)
{
if (ModelState.IsValid)
{
db.Osoba.Add(osoba);
db.Pracownik.Add(pracownik);
db.SaveChanges();
return RedirectToAction("Index");
}
My model class:
public partial class Pracownik
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id_osoby { get; set; }
public System.DateTime Data_zatrudnienia { get; set; }
public Nullable<System.DateTime> Data_zwolnienia { get; set; }
public double Pensja { get; set; }
public string Wyksztalcenie { get; set; }
public virtual Osoba Osoba { get; set; }
public virtual Pracownik_biurowy Pracownik_biurowy { get; set; }
public virtual Przewodnik Przewodnik { get; set; }
}
public partial class Osoba
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id_osoby { get; set; }
public string Imie { get; set; }
public string Nazwisko { get; set; }
public string Pesel { get; set; }
public virtual Pracownik Pracownik { get; set; }
}
Create form view:
#model BiuroPrototyp.Models.Pracownik
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Pracownik</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Osoba.Imie, htmlAttributes: new {#class = "control-label col-md-2"})
<div class="col-md-10">
#Html.EditorFor(model => model.Osoba.Imie, new {htmlAttributes = new {#class = "form-control"}})
#Html.ValidationMessageFor(model => model.Osoba.Imie, "", new {#class = "text-danger"})
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Osoba.Nazwisko, htmlAttributes: new {#class = "control-label col-md-2"})
<div class="col-md-10">
#Html.EditorFor(model => model.Osoba.Nazwisko, new {htmlAttributes = new {#class = "form-control"}})
#Html.ValidationMessageFor(model => model.Osoba.Nazwisko, "", new {#class = "text-danger"})
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Osoba.Pesel, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Osoba.Pesel, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Osoba.Pesel, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Data_zatrudnienia, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Data_zatrudnienia, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Data_zatrudnienia, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Pensja, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Pensja, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Pensja, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Wyksztalcenie, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Wyksztalcenie, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Wyksztalcenie, "", 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>
'Pracownik' class is employee and he is inherits from 'Osoba' which is Person in English. I dont know where I trying to put this id in my code.

You are trying to save an object to the database with an explicit ID set by you while the database is expecting to generate that value itself. That is the Id_osoby property in your object is set to something other than 0 and it is not identified to the EF framework as an identity field. If you want the Id to be generated by the database as your post suggests, then the corresponding property in the Object should be decorated with the [Key] attribute.

Related

Updating table issue in ASP.NET MVC

I have an ASP.NET MVC model class like this:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Customer
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Customer()
{
this.Rates_Comments = new HashSet<Rates_Comments>();
}
[Required]
public double ID { get; set; }
[Required]
public string Marital_Status { get; set; }
[Required]
public string Gender { get; set; }
[Required]
[Range(5000,500000)]
public double Income { get; set; }
[Required]
public double Children { get; set; }
[Required]
public string Education { get; set; }
[Required]
public string Occupation { get; set; }
[Required]
public string Home_Owner { get; set; }
View:
<h2>Update My Account:</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-inline">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.ID)
<div class="form-group" style="width:380px;">
<div class="form-group">
#Html.Label("Email-User Name", htmlAttributes: new { #class = "control-label col-md-10" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control", #style = "width:220px", #readonly = "readonly" } })
<br />
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.Label("First Name", htmlAttributes: new { #class = "control-label col-md-10" })
<div class="col-md-10">
#Html.EditorFor(model => model.F_Name, new { htmlAttributes = new { #class = "form-control", #style = "width:220px" } })
<br />
#Html.ValidationMessageFor(model => model.F_Name, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.Label("Last Name", htmlAttributes: new { #class = "control-label col-md-10" })
<div class="col-md-10">
#Html.EditorFor(model => model.L_Name, new { htmlAttributes = new { #class = "form-control", #style = "width:220px" } })
<br />
#Html.ValidationMessageFor(model => model.L_Name, "", new { #class = "text-danger" })
</div>
</div>
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Marital_Status,Gender,Income,Children,Education,Occupation,Home_Owner,Cars,Commute_Distance,Region,Age,Buy,OrderNumber")] Customer customer)
{
int[] Children = new int[] { 0, 1, 2, 3, 4, 5 };
ViewBag.OrderNumber = new SelectList(db.Orders, "OrderNumber", "OrderNumber");
ViewBag.Gender = new SelectList(db.Customers.Select(x => x.Gender).Distinct());
ViewBag.email = new SelectList(db.Customers.Select(x => x.Email).Distinct());
ViewBag.fn = new SelectList(db.Customers.Select(x => x.F_Name).Distinct());
ViewBag.ln = new SelectList(db.Customers.Select(x => x.L_Name).Distinct());
ViewBag.pas = new SelectList(db.Customers.Select(x => x.Pass).Distinct());
ViewBag.Marital_Status = new SelectList(db.Customers.Select(x => x.Marital_Status).Distinct());
ViewBag.Education = new SelectList(db.Customers.Select(x => x.Education).Distinct());
ViewBag.Occupation = new SelectList(db.Customers.Select(x => x.Occupation).Distinct());
ViewBag.Home_Owner = new SelectList(db.Customers.Select(x => x.Home_Owner).Distinct());
ViewBag.Cars = new SelectList(db.Customers.Select(x => x.Cars).Distinct());
ViewBag.Commute_Distance = new SelectList(db.Customers.Select(x => x.Commute_Distance).Distinct());
ViewBag.Region = new SelectList(db.Customers.Select(x => x.Region).Distinct());
ViewBag.Children = new SelectList(Children);
if (ModelState.IsValid)
{
db.Entry(customer).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.OrderNumber = new SelectList(db.Orders, "OrderNumber", "OrderNumber", customer.OrderNumber);
return View(customer);
}
My issue that because of the [Required] data annotations, I can't update one cell in my table for example, user have to access his account to edit it if he needs to, so he may only edit one filed, not all of it.
And due registration issue, all filed are required, so how to overcome this one?

ASP.NET MVC - Object reference not set to an instance of an object in DropDownList

I have a model Class
public partial class FEES
{
public FEES()
{
}
public long FEE_ID { get; set; }
public decimal AMOUNT { get; set; }
public int CURRENCY_ID { get; set; }
public string NAME { get; set; }
public virtual CURRENCIES CURRENCIES { get; set; }
}
ViewModel
public class FeesViewModel
{
public SelectList CurrenciesList { get; set; }
public FeesViewModelInput input { get; set; }
public class FeesViewModelInput
{
[HiddenInput]
public long FEE_ID { get; set; }
[Display(Name = "Amount")]
[Required(ErrorMessage = "Fee Amount Is Required!")]
[RegularExpression(#"^[0-9,.]+$", ErrorMessage = "Please enter proper currency format e.g. 2,500")]
public decimal AMOUNT { get; set; }
[Display(Name = "Currency")]
[Required(ErrorMessage = "Currency Is Required!")]
public int CURRENCY_ID { get; set; }
[Required(ErrorMessage = "Fee Name Is Required!")]
[Display(Name = "Fee Name")]
public string NAME { get; set; }
}
}
Small service for the ViewModel
public void createFees(FEES fee, FeesViewModel viewModel)
{
fee.FEE_ID = viewModel.input.FEE_ID;
fee.CURRENCY_ID = viewModel.input.CURRENCY_ID;
fee.NAME = viewModel.input.NAME.Trim();
}
I call the service and the ViewModel in my controller.
Controller
public ActionResult Create()
{
FeesViewModel fees = new FeesViewModel();
fees.CurrenciesList = new SelectList(_currenciesService.GetCurrencies().Where(c => c.ACTION_STATUS != 2), "CURRENCY_ID", "CURRENCY_NAME");
fees.FeeTypesList = new SelectList(_feetypesService.GetFeeTypes().Where(c => c.ACTION_STATUS != 2), "FEE_TYPE_ID", "FEE_TYPE_NAME");
return View();
}
[HttpPost]
public ActionResult Create(FeesViewModel fees)
{
try
{
if (ModelState.IsValid)
{
//check if values is duplicate
if (_feesService.GetFees().Where(c => c.ACTION_STATUS != 2).Any(c => c.NAME.ToLower().Trim() == fees.input.NAME.ToLower().Trim()))
{
this.AddNotification("Fee Name already exist.<br/> Kindly verify the data.", NotificationType.ERROR);
}
else
{
var fee = new BPP.CCSP.Admin.Web.BPPCCSPAdminFeesService.FEES();
var helper = new FeesService();
helper.createFees(fee, fees);
_feesService.AddFee(fee);
var notif = new UINotificationViewModel()
{
notif_message = "Record saved successfully",
notif_type = NotificationType.SUCCESS,
};
TempData["notif"] = notif;
return RedirectToAction("Index");
}
}
}
catch (Exception e)
{
this.AddNotification("Fees cannot be added.<br/> Kindly verify the data.", NotificationType.ERROR);
}
fees.CurrenciesList = new SelectList(_currenciesService.GetCurrencies().Where(c => c.ACTION_STATUS != 2), "CURRENCY_ID", "CURRENCY_NAME");
return View(fees);
}
And the View
#model BPP.CCSP.Admin.Web.ViewModels.FeesViewModel
#{
//ViewBag.Title = "Create";
}
<div class=" box box-body box-primary">
#using (Html.BeginForm("Create", "Fees", FormMethod.Post, new { #class = "form-horizontal", #enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true, null, new { #class = "text-danger" })
#*#Html.HiddenFor(model => model.faculty_activation_date, new { #Value = System.DateTime.Now })*#
<div class="row .col">
<div style="margin-top:20px" class="mainbox col-md-12 col-md-offset-0 col-sm-8 col-sm-offset-2">
<div class="panel panel-info">
<div class="panel-heading">
<div class="panel-title">Create Fee</div>
</div>
<div class="panel-body">
<div class="col-md-6">
<div>
#Html.LabelFor(model => model.input.NAME, "Fee Name")
#Html.TextBoxFor(model => model.input.NAME, new { #style = "border-radius:3px;", #type = "text", #class = "form-control", #placeholder = Html.DisplayNameFor(m => m.input.NAME), #autocomplete = "on" })
#Html.ValidationMessageFor(model => model.input.NAME, null, new { #class = "text-danger" })
</div>
<div>
#Html.LabelFor(model => model.input.AMOUNT, "Amount")
#Html.TextBoxFor(model => model.input.AMOUNT, new { #style = "border-radius:3px;", #type = "text", #class = "form-control", #placeholder = Html.DisplayNameFor(m => m.input.AMOUNT), #autocomplete = "on" })
#Html.ValidationMessageFor(model => model.input.AMOUNT, null, new { #class = "text-danger" })
</div>
</div>
<div class="col-md-6">
<div>
#Html.LabelFor(model => model.input.CURRENCY_ID, "Currency")
#*#Html.DropDownList("CURRENCY_ID", (IEnumerable<SelectListItem>)ViewBag.name, "Please Select a Currency", new { #class = "form-control", #style = "border-radius:3px;" })*#
#Html.DropDownListFor(x => x.input.CURRENCY_ID, Model.CurrenciesList, "Please Select a Currency", new { #class = "form-control", #style = "border-radius:3px;" })
#Html.ValidationMessageFor(model => model.input.CURRENCY_ID, null, new { #class = "text-danger" })
</div>
<div>
#Html.LabelFor(model => model.input.FEE_TYPE_ID, "Fee Type")
#Html.DropDownListFor(model => model.input.FEE_TYPE_ID, Model.FeeTypesList, "Please Select a Fee Type", new { #class = "form-control", #style = "border-radius:3px;" })
#Html.ValidationMessageFor(model => model.input.FEE_TYPE_ID, null, new { #class = "text-danger" })
</div>
</div>
</div>
<div class="panel-footer">
<div class="panel-title">
<div class="form-actions no-color">
<input type="submit" value="Create" class="btn btn-success" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
}
</div>
}
When I clicked on the View (Create), I got this error
The CurrencyID is a DropDownList coming from CURRENCIES model class.
I have these questions:
Why am I getting this error and how do I resolve it.
How do I do ViewModel without mapping.?
Why am I getting this error and how do I resolve it.
Because the Model is not set in your view. It is null.
When the users visit the Create page, you need to make sure to present them with options in the dropdown. Therefore, you need to make sure you pass the model into the view during GET.
public ActionResult Create()
{
// your code and pass fees to your view.
return View(fees);
}
How do I do ViewModel without mapping. Any example please.
You can use AutoMapper NuGet package to do the mapping.

MVC - Foreign Key Error because of ViewModel?

I am Creating View Model of Employee Class and strongly typed my Create View with the EmployeeViewModel. In future, I will add many classes in my View Model. But the problem is I am getting Gender Foreign Key Error. May be I am binding wrong values in Create Controller. Below is my code:
Create Controllers:
public ActionResult Create()
{
ViewBag.GenderId = new SelectList(db.Genders, "Id", "Name");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(EmployeeViewModel employeeModel)
{
if (ModelState.IsValid)
{
db.Employees.Add(employeeModel.Employee);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.GenderId = new SelectList(db.Genders, "Id", "Name", employeeModel.Employee.GenderId);
return View(employeeModel);
}
Create View:
#model WebApplication2.EmployeeViewModel
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<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.GenderId, "GenderId", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("GenderId", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Employee.GenderId, "", 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>
}
Models:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public byte GenderId { get; set; }
public Gender Gender { get; set; }
}
public class Gender {
public byte Id { get; set; }
public string Name { get; set; }
}
View Model:
public class EmployeeViewModel
{
public Employee Employee { get; set; }
}

How to retrieve Posted file in create action of another entity

I am in trouble with a posted file related to an entity, I can see the posted file in the request, but if I add the httppostedfile as action parameter it will be null... what should I do to manage this case?
NewsArticle model:
public class NewsArticle
{
public string Title{get; set;}
public int ID{get; set;}
[AllowHtml]
public string Body { get; set; }
public Image Image { get; set; }
//other stuff
}
where the field Image is:
the related entity will be created in the newarticlecontroller
public class Image
{
public int ID { get; set; }
public string URL { get; set; }
public string Title { get; set; }
public string Subtitle { get; set; }
}
detail of view
#model GatorsWebSite.NewsArticle
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm("Create", "NewsArticles", FormMethod.Post, new { enctype = "multipart/forma-data" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>NewsArticle</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.Body, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextAreaFor(model => model.Body, new { htmlAttributes = new { #class = "form-control ckHolder" } })
#Html.ValidationMessageFor(model => model.Body, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SubTitle, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SubTitle, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SubTitle, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Image, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Image, new { type = "file" })
#Html.ValidationMessageFor(m => m.Image)
</div>
</div>
#*<div class="form-group">
#Html.LabelFor(model => model.AuthorID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.AuthorID, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.AuthorID, "", new { #class = "text-danger" })
</div>
</div>*#
#*<div class="form-group">
#Html.LabelFor(model => model.Date, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Date, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Date, "", new { #class = "text-danger" })
</div>
</div>*#
<div class="form-group">
#Html.LabelFor(model => model.Published, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.Published)
#Html.ValidationMessageFor(model => model.Published, "", 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", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
#Scripts.Render("~/bundles/ckEditor")
<script type="text/javascript">
CKEDITOR.replace("#Html.IdFor(m => m.Body)", {});
</script>
}
Controller action:
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Title,Body,SubTitle,Published,Image")]NewsArticle newsArticle )// here I don't know how to manage the posted file
{
if (ModelState.IsValid)
{
newsArticle.Date = DateTime.Now;
newsArticle.AuthorID = User.Identity.GetUserId();
newsArticle.Body = newsArticle.Body;
_repository.Add(newsArticle);
return RedirectToAction("Index");
}
return View(newsArticle);
}
Any help will be really appreciated
And what if you put input file in your form like:
<div class="form-group">
<label for="newsimage">Select news image:</label>
<div class="col-md-10">
<div class="checkbox">
<input id="newsimage" type="file" name="newsimage"/>
</div>
</div>
</div>
and post action will be:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Title,Body,SubTitle,Published,Image")]NewsArticle newsArticle )// here I don't know how to manage the posted file
{
if (ModelState.IsValid)
{
string ImageUrl="";
for (int i = 0; i < Request.Files.Count; i++)
{
if (Request.Files[i].FileName!="")
{
string UrlFile = Server.MapPath("~/newsimagefolderpath/") + FileName;
Request.Files[i].SaveAs(UrlFile);
ImageUrl = UrlFile;
}
}
newsArticle.Date = DateTime.Now;
newsArticle.URL = string.IsNullOrEmpty(ImageUrl) ? "defaultimagepath" : ImageUrl;
newsArticle.AuthorID = User.Identity.GetUserId();
newsArticle.Body = newsArticle.Body;
_repository.Add(newsArticle);
return RedirectToAction("Index");
}
return View(newsArticle);
}
Ok, I am new to MVC so obviously I have to pay the tax, btw this is what I have done to solve this problem:
I have created a ViewModel to host the aggregated data of the view:
public class NewsArticleViewModel
{
public string Title { get; set; }
public int ID { get; set; }
[AllowHtml]
public string Body { get; set; }
public string SubTitle { get; set; }
public string AuthorID { get; set; }
public DateTime Date { get; set; }
public bool Published { get; set; }
public HttpPostedFileBase ImageUpload { get; set; }
}
then I have changed the signature of the control action:
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Title,Body,SubTitle,Published,ImageUpload")]GatorsWebSite.ViewModels.NewsArticleViewModel newsArticle)
then I have corrected a typo in the form declaration :)
#using (Html.BeginForm("Create", "NewsArticles", FormMethod.Post, new { enctype = "multipart/form-data" }))
...and then I have changed the view
#Html.LabelFor(model => model.ImageUpload, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.ImageUpload, new { type = "file" })
#Html.ValidationMessageFor(m => m.ImageUpload)
</div>
Result:

Save an image to a database in MVC 4

I am trying to get an image file name and location saved into a database, using mvc 4. Can someone see where I am going wrong? I am only new to mvc, and building this for a client. I have bplace a break point on this line "if (Request.Files.Count > 0)", but it skips it and goes into my else statement.
public ActionResult Create2(FestivalVM model)
{
if (ModelState.IsValid != true)
{
if (model.SelectedFestivalType != -1)
{
//db.save stuff from create.
Festival Newfestival = new Festival();
Newfestival.EndDate = model.endDate.Date;
Newfestival.FestivalCounty = db.Counties.Where(p => p.ID == model.SelectedCounty).Single();
Newfestival.FestivalName = model.FestivalName;
Newfestival.Description = model.sDescription;
Newfestival.FType = db.FestivalTypes.Where(p => p.ID == model.SelectedFestivalType).Single();
Newfestival.StartDate = model.startDate.Date;
Newfestival.Location = model.Location;
Newfestival.FestivalTown = db.Towns.Where(p => p.ID == model.SelectedTown).Single();
Newfestival.UserID = WebSecurity.CurrentUserId;
if (Request.Files.Count > 0)
{
string fileName = Guid.NewGuid().ToString();
string serverPath = Server.MapPath("~\\Content\\FestivalLogo");
Bitmap newImage = new Bitmap(Request.Files[0].InputStream);
newImage.Save(serverPath + "\\" + fileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
model.festivalLogo = "Content/FestivalLogo/" + fileName + ".jpg";
db.Festivals.Add(Newfestival);
db.SaveChanges();
return RedirectToAction("Details", new {id = Newfestival.FestivalId});
}
else
{
db.Festivals.Add(Newfestival);
db.SaveChanges();
return RedirectToAction("Details", new { id = Newfestival.FestivalId });
}
}
ModelState.AddModelError("", "No Festival Type Picked");
}
model.County = db.Counties.ToDictionary(p => p.ID, q => q.Name);
model.FestivalType = db.FestivalTypes.ToDictionary(p => p.ID, q => q.FType);
model.FestivalType.Add(-1, "--- Add New Festival Type ---");
model.Towns = db.Towns.ToDictionary(p => p.ID, q => q.Name);
model.startDate = DateTime.Now;
model.endDate = DateTime.Now;
return View(model);
}
View- Edited Here
#model MyFestival.Models.FestivalVM
#{
ViewBag.Title = "Add a Festival";
Layout = "~/Views/Shared/Festival.cshtml";
}
<h2>Create Your Festival</h2>
<br />
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<ol class="breadcrumb">
<li>Home</li>
<li class="active">Creating a Festival</li>
</ol>
<hr />
#Html.ValidationSummary(true, null, new{#class="alert alert-danger"})
<div class="form-group">
#Html.LabelFor(model => model.FestivalName, new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-info-sign"></i></span>
#Html.TextBoxFor(model => model.FestivalName, new { #class = "form-control", #style = "width:210px" })
</div>
#Html.ValidationMessageFor(model => model.FestivalName, null, new { #style = "color:red;" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.startDate, new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
#Html.TextBoxFor(model => model.startDate, new { #class = "form-control datepicker", #style = "width:250px" })
</div>
#Html.ValidationMessageFor(model => model.startDate, null, new { #style = "color:red;" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.endDate, new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
#Html.TextBoxFor(model => model.endDate, new { #class = "form-control datepicker", #style = "width:250px" })
</div>
<!--<input class="form-control datepicker" style="width:250px" name="endDate" placeholder="Please pick date..."/>-->
#Html.ValidationMessageFor(model => model.endDate, null, new { #style = "color:red;" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Towns, new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-tag"></i></span>
#Html.DropDownListFor(p => p.SelectedTown, Model.Towns.Select(p => new SelectListItem()
{
Text = p.Value.ToString(),
Value = p.Key.ToString(),
Selected = false
}),
new
{
#class = "form-control",
#style = "width:210px",
placeholder = "---- Select a Town ----"
})
</div>
#Html.ValidationMessageFor(model => model.Towns, null, new { #style = "color:red;" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.County, new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-tag"></i></span>
#Html.DropDownListFor(p => p.SelectedCounty, Model.County.Select(p => new SelectListItem()
{
Text = p.Value.ToString(),
Value = p.Key.ToString(),
Selected = false
}),
new
{
#class = "form-control",
#style = "width:210px"
})
</div>
#Html.ValidationMessageFor(model => model.County, null, new { #style = "color:red;" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FestivalType, new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-tag"></i></span>
#Html.DropDownListFor(p => p.SelectedFestivalType, Model.FestivalType.Select(p => new SelectListItem()
{
Text = p.Value.ToString(),
Value = p.Key.ToString(),
Selected = false
}),
new
{
#class = "form-control",
#style = "width:210px;",
#onchange = "checkaddnew();"
})
</div>
#Html.ValidationMessageFor(model => model.FestivalType, null, new { #style = "color:red;" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.sDescription, new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-info-sign"></i></span>
#Html.TextAreaFor(model => model.sDescription, new { #class = "form-control", #style = "width:210px;" })
</div>
#Html.ValidationMessageFor(model => model.sDescription, null, new { #style = "color:red;" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Location, new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-info-sign"></i></span>
#Html.TextAreaFor(model => model.Location, new { #class = "form-control", #style = "width:210px" })
</div>
#Html.ValidationMessageFor(model => model.Location, null, new { #style = "color:red;" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.festivalLogo, new{#class="control-label col-md-2"})
<div class="col-md-10">
<input type="file" id="imageFile" name="imageFile" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-info" />
#Html.ActionLink("Back to List", "Index", null, new { #class = "btn btn-danger" })
</div>
</div>
</div>
}
#Html.Partial("CreateFestivalType", new MyFestival.Models.FestivalTypeVM())
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
<script>
$(document).ready(function () {
$('#SelectedFestivalType').change(function () {
if ($(this).find(":selected").val() == -1) {
$('#myModal').modal('show');
}
});
});
</script>
<script type="text/javascript">
function ajaxResponse(data) {
alert("This Worked and the Data ID is: " + data.FestivalTypeID);
var newOption = "<option value='" + data.FestivalTypeID + "'>" + data.Name + "</option>";
$('#SelectedFestivalType').append(newOption);
$('#myModal').modal('hide');
$("#SelectedFestivalType option[value='" + data.FestivalTypeID + "']").attr("selected", "selected");
}
;
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#startDate").datepicker("setDate", '+1', { dateFormat: "dd/mm/yy" }).on('changeDate', function (ev) {
$(this).blur();
$(this).datepicker('hide');
});
});
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#endDate").datepicker("setDate", '+2', { dateFormat: "dd/mm/yy" }).on('changeDate', function (ev) {
$(this).blur();
$(this).datepicker('hide');
});
});
</script>
Model
public class Festival
{
[Key]
public int FestivalId { get; set; }
[Required]
[Display(Name = "Festival Name"), StringLength(100)]
[DisplayFormat(ApplyFormatInEditMode = true)]
public string FestivalName { get; set; }
[Required]
[Display(Name = "Start Date")/*, DataType(DataType.Date)*/]
[DisplayFormat(DataFormatString = "{0:dd/MMM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime StartDate { get; set; }
[Required]
[Display(Name = "End Date")/*, DataType(DataType.Date)*/]
[DisplayFormat(DataFormatString = "{0:dd/MMM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime EndDate { get; set; }
[Display(Name = "Festival Location")]
[DisplayFormat(ApplyFormatInEditMode = true)]
public DbGeography Location { get; set; }
[Required]
[Display(Name = "County")]
[DisplayFormat(ApplyFormatInEditMode = true)]
public virtual County FestivalCounty { get; set; }
[Required]
[Display(Name = "Town")]
[DisplayFormat(ApplyFormatInEditMode = true)]
public virtual Town FestivalTown { get; set; }
[Required]
[Display(Name = "Festival Type")]
[DisplayFormat(ApplyFormatInEditMode = true)]
public virtual FestivalType FType { get; set; }
[Display(Name = "Festival Logo")]
[DataType(DataType.Upload)]
[DisplayFormat(ApplyFormatInEditMode = true)]
public string FestivalLogo { get; set; }
[Display(Name = "Description"), StringLength(200)]
public string Description { get; set; }
public ICollection<Events> Events { get; set; }
public IEnumerable<Events> EventsOrdered
{
get { return Events.OrderBy(e => e.EventsDate); }
}
public int UserID { get; set; }
[ForeignKey("UserID")]
public virtual UserProfile User { get; set; }
}
ViewModel
public class FestivalVM
{
public int FestivalID { get; set; }
[Required]
[Display(Name = "Town")]
[DisplayFormat(ApplyFormatInEditMode = true)]
public Dictionary<int, string> Towns { get; set; }
[Required]
[Display(Name = "County")]
[DisplayFormat(ApplyFormatInEditMode = true)]
public Dictionary<int, string> County { get; set; }
[Required]
[Display(Name = "Festival Type")]
[DisplayFormat(ApplyFormatInEditMode = true)]
public Dictionary<int, string> FestivalType { get; set; }
public int SelectedTown { get; set; }
public int SelectedCounty { get; set; }
public int SelectedFestivalType { get; set; }
[Required]
[Display(Name = "Festival Name"), StringLength(100)]
[DisplayFormat(ApplyFormatInEditMode = true)]
public string FestivalName { get; set; }
[Required]
[Display(Name = "Start Date")/*, DataType(DataType.Date)*/]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime startDate { get; set; }
[Required]
[Display(Name = "End Date")/*, DataType(DataType.Date)*/]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime endDate { get; set; }
public HttpPostedFileWrapper imageFile { get; set; }
[Display(Name = "Festival Logo")]
public string festivalLogo { get; set; }
public UserProfile UserID { get; set; }
[Display(Name = "Description"), StringLength(200)]
public string sDescription { get; set; }
[Required]
[Display(Name = "Location")]
public DbGeography Location { get; set; }
}
You can do some changes to allow posting data:
View
change #using (Html.BeginForm()) to this line: using (Html.BeginForm("Create2", "YourControllerName", FormMethod.Post, new {enctype="multipart/form-data"})) as you are posting data
Action
should be like this:
public ActionResult Create2(FestivalVM model, HttpPostedFileBase imageFile)
{
if (ModelState.IsValid != true)
{
if (model.SelectedFestivalType != -1)
{
//db.save stuff from create.
Festival Newfestival = new Festival();
Newfestival.EndDate = model.endDate;
Newfestival.FestivalCounty = db.Counties.Where(p => p.ID == model.SelectedCounty).Single();
Newfestival.FestivalName = model.FestivalName;
Newfestival.Description = model.sDescription;
Newfestival.FType = db.FestivalTypes.Where(p => p.ID == model.SelectedFestivalType).Single();
Newfestival.StartDate = model.startDate;
Newfestival.Location = model.Location;
Newfestival.FestivalTown = db.Towns.Where(p => p.ID == model.SelectedTown).Single();
Newfestival.UserID = WebSecurity.CurrentUserId;
if (Request.Files.Count > 0)
{
string fileName = Guid.NewGuid().ToString();
string serverPath = Server.MapPath("~\\Content\\FestivalLogo");
Bitmap newImage = new Bitmap(Request.Files["imageFile"].InputStream);
newImage.Save(serverPath + "\\" + fileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
Newfestival.festivalLogo = "Content/FestivalLogo/" + fileName + ".jpg";
db.Festivals.Add(Newfestival);
db.SaveChanges();
return RedirectToAction("Details", new {id = Newfestival.FestivalId});
}
else
{
db.Festivals.Add(Newfestival);
db.SaveChanges();
return RedirectToAction("Details", new { id = Newfestival.FestivalId });
}
}
ModelState.AddModelError("", "No Festival Type Picked");
}
model.County = db.Counties.ToDictionary(p => p.ID, q => q.Name);
model.FestivalType = db.FestivalTypes.ToDictionary(p => p.ID, q => q.FType);
model.FestivalType.Add(-1, "--- Add New Festival Type ---");
model.Towns = db.Towns.ToDictionary(p => p.ID, q => q.Name);
model.startDate = DateTime.Now;
model.endDate = DateTime.Now;
return View(model);
}
I hope it will help