Im new to asp.net MVC4 , I have an MVC4 application that is using SQL Server 2012 EntityFramework (code first). The Edit post method is not saving the data. On checking if ModelState.IsValid it return false, can anyone help me find whats wrong with my code
MODEL
public class Customer
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage="*")]
public string FirstName { get; set; }
[Required(ErrorMessage = "*")]
public string LastName { get; set; }
[Required(ErrorMessage = "*")]
[MaxLength(1, ErrorMessage="Initial only")]
public string MI { get; set; }
[Required(ErrorMessage = "*")]
public string Address { get; set; }
[Required(ErrorMessage = "*")]
public String ContactNo { get; set; }
[Required(ErrorMessage = "*")]
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email")]
public string EmailAddress { get; set; }
[Required(ErrorMessage = "*")]
[MaxLength(8, ErrorMessage = "Max of 8")]
[MinLength(5, ErrorMessage = "Min of 5")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[NotMapped]
public string Password { get; set; }
[Required(ErrorMessage = "*")]
[DataType(DataType.Password)]
[NotMapped]
[Display(Name = "Retype-PW")]
public string RetypePassword { get; set; }
[Required]
[Display(Name = "How much is")]
[NotMapped]
public string Captcha { get; set; }
}
EDIT VIEW
<h2>Edit CUSTOMER</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Customer</legend>
#Html.HiddenFor(model => model.Id)
#Html.HiddenFor(model=>model.Captcha)
#Html.HiddenFor(model=>model.Password)
#Html.HiddenFor(model=>model.RetypePassword)
<div class="editor-label">
#Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstName)
#Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.MI)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MI)
#Html.ValidationMessageFor(model => model.MI)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Address)
#Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ContactNo)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ContactNo)
#Html.ValidationMessageFor(model => model.ContactNo)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.EmailAddress)
#Html.ValidationMessageFor(model => model.EmailAddress)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
EDIT CONTROLLER METHOD
public ActionResult Edit(int id = 0)
{
Customer customer = db.Customers.Find(id);
if (customer == null)
{
return HttpNotFound();
}
return View(customer);
}
//
// POST: /Customer/Edit/5
[HttpPost]
public ActionResult Edit(Customer customer)
{
if (ModelState.IsValid)
{
db.Entry(customer).State = EntityState.Modified;
db.SaveChanges();
if (User.IsInRole("Administrator"))
{
return RedirectToAction("Index");
}
else
{
return RedirectToAction("Details");
}
}
return View(customer);
}
Thanks for the help
#Html.HiddenFor(model=>model.Captcha)
#Html.HiddenFor(model=>model.Password)
#Html.HiddenFor(model=>model.RetypePassword)
this lines are not safety, it's possible view the content in console at the same time maybe isn't the good value in password you make a 8 char limit but you read the value in bd, i hope this value is cripted!, you write a code to more to 8 char well the model isn't valid, try to read the value for this champs in the console and compare with your settings.
the problem you use the same model to create and edit, well for edit isn't required the captha and pasword, the options are make other model, setting not required, check if create or edit mode to set required,
help me find whats wrong with my code
You're using the scaffolded #Html.ValidationSummary(true), where true is passed for hidePropertyErrors. This is fine if your edit form shows an editor for each property of your model, but if not, your page will not show what ModelState errors prevented successful processing. Change true to false to see all errors.
You're using Entity Framework models as ViewModels.
You're allowing mass assignment.
You're not using an cross-site request forgery token.
You've just run into the prime case that demonstrates why you should use ViewModels. One solution is to remove model errors for properties you don't want to see as faulty.
Like:
ModelState.Remove("Captcha");
ModelState.Remove("Password");
ModelState.Remove("RetypePassword");
if (ModelState.IsValid)
{
// your code ...
But this will cause your model to be updated with an empty password null for the properties for which you didn't provide an editor, which you don't want to and which will fail anyway. So you'll need to load the existing entity and update it:
ModelState.Remove("Captcha");
ModelState.Remove("Password");
ModelState.Remove("RetypePassword");
if (ModelState.IsValid)
{
var existingCustomer = db.Customers.First(c => c.ID == customer.ID);
// Update properties of attached entity
existingCustomer.FirstName = customer.FirstName;
existingCustomer.LastName = customer.LastName;
// and so on...
To circumvent most of this clumsiness, simply define a ViewModel:
public class EditCustomerModel
{
[Required(ErrorMessage="*")]
public string FirstName { get; set; }
[Required(ErrorMessage="*")]
public string LastName { get; set; }
// and so on...
}
Then in your action method will look like this:
public ActionResult Edit(EditCustomerModel customer)
{
if (ModelState.IsValid)
{
var existingCustomer = db.Customers.First(c => c.ID == customer.ID);
// Update properties of attached entity
existingCustomer.FirstName = customer.FirstName;
existingCustomer.LastName = customer.LastName;
db.SaveChanges();
return RedirectToAction(...);
}
return View();
}
I used CodeCaster 2nd suggestion but it didn't work, but when I put hidden input for my 3 notmapped fields(Captcha, password, retypepassword) on my view and assigning default values it works now.
<input type="hidden" id="Captcha" name="Captcha" value="Captcha" />
<input type="hidden" id="Password" name="Password" value="Password" />
<input type="hidden" id="RetypePassword" name="RetypePassword" value="RetypePassword" />
Related
I am using MVC in order to build a blog. What I want is to save post comments to its corresponding place in the database but it does not work.
My post model is as follows:
public class Post
{
[Key]
public int PostId { get; set; }
public string Title { get; set; }
public DateTime CreatedDate{get;set;}
public DateTime UpdateDate { get; set; }
public string Body { get; set; }
public ICollection<Comment> Comments { get; set;}
public ICollection<Tag> Tags { get; set; }
}
My Comment model is as follows:
public class Comment
{
[Key]
public int CommentId { get; set; }
public int PostId { get; set; }
[ForeignKey("PostId")]
public virtual Post Post{get; set;}
public string CommentCreateDate { get; set; }
public string CommentUpdateDate { get; set; }
public string CommeterName { get; set; }
public string EmailAddress { get; set; }
public string CommentText { get; set; }
public bool Approved { get; set; }
}
I have the following Action Methods:
[HttpGet]
public ActionResult CreateComment()
{
return View();
}
[HttpPost]
[ValidateInput(false)]
public ActionResult CreateComment(int id, string name, string email, string txt, bool aproved = false)
{
Post post = GetPost(id);
Comment comment = new Comment();
comment.Post = post;
comment.CommentCreateDate = DateTime.Now.ToString();
comment.CommeterName = name;
comment.EmailAddress = email;
comment.CommentText = txt;
comment.Approved = aproved;
db.Comments.Add(comment);
db.SaveChanges();
m_commentList.Add(comment);
return RedirectToAction("CreateComment", new { id = id });
}
And in my view I am trying this:
#model Blog.Models.Comment
#{
ViewBag.Title = "CreateComment";
}
<h2>Create a Comment</h2>
#using (Html.BeginForm())
{
<fieldset>
<legend>Enter Comment</legend>
<div class="editor-label">
#Html.LabelFor(model => model.CommeterName)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.CommeterName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.EmailAddress)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CommentText)
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.CommentText)
</div>
<p>
<input type="submit" value="Create comment" />
</p>
</fieldset>
}
I get no exception but none of the data from the form is being saved. Only the data that is set in the action result, that is, CommentCreateDate and Approved. I am not sure what I am doing wrong.
I have tried a second option which is to include the id of the comment in BeginForm() as follows:
#using (Html.BeginForm(new {id = Model.CommentId}))
{
<fieldset>
<legend>Enter Comment</legend>
<div class="editor-label">
#Html.LabelFor(model => model.CommeterName)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.CommeterName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.EmailAddress)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CommentText)
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.CommentText)
</div>
<p>
<input type="submit" value="Create comment" />
</p>
</fieldset>
}
This will give me a null reference exception even if I use the new keyword :
System.NullReferenceException: Object reference not set to an instance of an object.
Why is this happening? Can anybody help?
Thank you
Your action signature should be:
public ActionResult CreateComment(Comment model)
The names generated for the form fields will bind back to the properties of the same model class. There's no way for the framework to know, for example, that the CommenterName property should match up to the name parameter of the action.
Your second example makes very little sense - you're trying to write out the ID but you have never set one. In fact, you don't even pass a Comment to the view with the form, which is why you get a NullReferenceException:
[HttpGet]
public ActionResult CreateComment()
{
return View();
}
Also, you should be careful with what fields you expose to your models and actions. For example, a user could easily force their comment to be approved just by adding the following markup via their browser's development console:
<input type="hidden" name="approved" value="true" />
Anything that is either in your model properties or a parameter to your action can be set by the user.
An altogether better option would be to use a dedicated model class for the form:
public class CreateCommentViewModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Text { get; set; }
}
Then to map this to your Comment in your action with:
[HttpPost]
public ActionResult CreateComment(CommentViewModel model)
{
var comment = new Comment();
comment.CommenterName = model.Name;
// etc...
}
This prevents the user from being able to set things like Approved and CreatedDate.
I suffer from the following there
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Posts</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Title)
#Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
<h4>Теги:</h4>
</div>
<div class="editor-field">
<input type="text" name="tags" value="#ViewBag.Tags"/><br />
</div>
#Html.HiddenFor(model => model.DateTime, new { #Value = System.DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss") })
#Html.ValidationMessageFor(model => model.DateTime)
<h5>body:</h5><textarea name="body" rows="10" cols="80" required >
</textarea><br />
<div class="editor-label">
#Html.LabelFor(model => model.Avtor)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Avtor)
#Html.ValidationMessageFor(model => model.Avtor)
</div>
<p>
<input type="submit" value="save" />
</p>
</fieldset>
}
in
public ActionResult Create([Bind(Include = "Id,Title,DateTime,Body,Avtor")] Posts post, string tags, string body)
{
if (ModelState.IsValid)
{
tags = tags ?? string.Empty;
string[] tagNames = tags.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string tagName in tagNames)
{
post.Tags.Add(GetTag(tagName));
}
post.Body = body;
db.Posts.Add(post);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(post);
}
public partial class Posts
{
public Posts()
{
this.Comments = new HashSet<Comments>();
this.Tags = new HashSet<Tags>();
}
public int Id { get; set; }
[Required]
[StringLength(64, MinimumLength = 1)]
public string Title { get; set; }
public System.DateTime DateTime { get; set; }
[Required]
[AllowHtml]
public string Body { get; set; }
[Required]
[StringLength(64, MinimumLength = 1)]
public string Avtor { get; set; }
public virtual ICollection<Comments> Comments { get; set; }
public virtual ICollection<Tags> Tags { get; set; }
}
if the user dials in plain text in textarea, it's okay, but there is a need to add the code text. It makes <pre> and <code>but it can only do I have in the program, not the user in textarea. Strictly speaking how to do so in tehtarea could write code and save it in the database and not get an error using <a> <script> and that they could be seen in the text. so the spacecraft itself is implemented on this site. Thanks in advance.
Create a strongly typed model to accept the data posted to the server, use the data annotation AllowHtml to accept HTML as input:
public class SomeViewModel
{
Posts Post { get; set; }
[AllowHtml]
public string Body { get; set; }
}
Then change the controller to
public ActionResult Create(SomeViewModel model)
{
if (ModelState.IsValid)
{
model.Post.Body = model.Body;
db.Posts.Add(model.Post);
db.SaveChanges();
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
Also try adding this to the following location in your web.config file just above the pages section:
<system.web>
...
<httpRuntime requestValidationMode="2.0" enableVersionHeader="false" />
<pages...
...
</system.web>
Changing the request validation mode will essentially only validation pages rather than every http request. You can read more about it here on MSDN HttpRuntimeSection.RequestValidationMode
Use AllowHTMLAttribute
see this MSDN
This will only allow html for that specific field in your viewmodel but not the whole thing.
or alternatively you can do:
[ValidateInput(false)]
public ActionResult someAction(SomeViewModel model)
{
//Your code to handle the control
}
EDIT: Notice that your textarea is not part of the model in your HTML, can you change that <textarea name="body".... line to:
#Html.TextArea("body", new { rows=10, columns=80 })
After that your body textarea element should be able to capture HTML elements to your controller.
Model:
faculty class
[Key]
[Required(ErrorMessage="*Enter Faculty id")]
[StringLength(5)]
public string Id { get; set; }
[Required(ErrorMessage="*Enter First Name")]
[MaxLength(30)]
public string F_Name { get; set; }
[Required(ErrorMessage="*Enter Last Name")]
[MaxLength(30)]
public string L_Name { get; set; }
[MaxLength(30)]
public string M_Name { get; set; }
[Required(ErrorMessage="*Enter Email id")]
[MaxLength(50)]
public string Email{ get; set; }
[Required(ErrorMessage="*Enter Department")]
public Int16 Dept_Id { get; set; }
[ForeignKey("Dept_Id")]
public virtual Department Dept { get; set; }
Department class:
public class Department //static table
{
[Key]
public Int16 Id { get; set; }
[Required]
[MaxLength(20)]
public string Dept_Name { get; set; }
}
View:
<div class="editor-label">
Faculty Id
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.Id, new { style = "width:200px;"})
#Html.ValidationMessageFor(model => model.Id,null, new { style="color:Red"})
</div>
<div class="editor-label">
First Name
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.F_Name,new { style = "width:200px;"})
#Html.ValidationMessageFor(model => model.F_Name, null, new { style="color:red;"})
</div>
<div class="editor-label">
Middle Name
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.M_Name,new { style = "width:200px;"})
#Html.ValidationMessageFor(model => model.M_Name)
</div>
<div class="editor-label">
Last Name
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.L_Name,new { style = "width:200px;"})
#Html.ValidationMessageFor(model => model.L_Name, null, new { style="color:red;"})
</div>
<div class="editor-label">
Department
</div>
<div class="editor-field">
#Html.DropDownListFor(model =>model.Dept_Id ,ViewBag.Dept_Id as IEnumerable<SelectListItem>,
string.Empty,new { style = "width:200px;text-align:center;"})
#Html.ValidationMessageFor(model => model.Dept_Id, null, new { style="color:red;"})
</div>
<p>
<input type="submit" value="Create" />
</p>
Controller:
[HttpPost]
public ActionResult Create(Faculty faculty)
{
faculty.Email = faculty.F_Name + "." + faculty.L_Name + "#mitcoe.edu.in";
if (ModelState.IsValid)
{
db.Faculty.Add(faculty);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Dept_Id = new SelectList(db.Department, "Id", "Dept_Name", faculty.Dept_Id);
return View(faculty);
}
The drop down list works fine and the ID for all the fields is fetched properly from the view.
But in the post function the modelstate is invalid as the Dept_name is null.
ORM creates the tables with only Dept_Id as a foreign key.
Why is the controller expecting Dept_Name also? Is anything wrong with my models?
The controller is expecting Dept_Name because:
You are model binding to the Faculty class.
Department is a component of Faculty.
Dept_Name is a required field on Department.
Dept_Name is null when the model binding parses the form data,
as you do not have it as an input anywhere in you view.
Two suggestions, either:
Have a separate FacultyInputModel class which is
the parameter to the Create action method. FacultyInputModel includes only those properties which you expect to be returned from the form;
Or: Use HiddenFor with the Dept_Name property, so it is included in your form data posted from you view and the model state is valid.
I'd recommend the first personally. It can be useful sometimes to separate your view models, i.e. what data you are displaying, from your input models, i.e. what data you expect to be posted back. It does add more classes and complexity though, on the down side. See e.g. https://www.simple-talk.com/dotnet/asp.net/the-three-models-of-asp.net-mvc-apps/ for a bit about input models.
I'm working with ASP.NET MVC 4 and I have the following models:
public class MultiLanguageText
{
[Key] public int Id { get; set; }
[Required] public string Name { get; set; }
public virtual ICollection<LocalizedText> LocalizedTexts { get; set; }
}
public class LocalizedText
{
[Key] public int Id { get; set; }
[ForeignKey("Language")] public int LanguageId { get; set; }
public virtual Language Language { get; set; }
[ForeignKey("MultiLanguageText")] public int MultiLanguageTextId { get; set; }
public virtual MultiLanguageText MultiLanguageText { get; set; }
public string Text { get; set; }
}
I'm trying to create an Edit view for MultiLanguageText which will also contain an editor for every item in the LocalizedTexts property. The form goes like this:
#using (Html.BeginForm("Edit", "MultiLanguageText", FormMethod.Post)) {
#Html.ValidationSummary(true)
<fieldset>
<legend>MultiLanguageText</legend>
#Html.HiddenFor(model => model.Id)
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
#Html.EditorFor(model => model.LocalizedTexts)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
The editor template I've created (in the file ~/Views/Shared/EditorTemplates/LocalizedText.cshtml) is the following:
<fieldset>
<legend>LocalizedText</legend>
#Html.HiddenFor(model => model.Id)
<div class="editor-label">
#Html.LabelFor(model => model.LanguageId)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LanguageId)
#Html.ValidationMessageFor(model => model.LanguageId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.MultiLanguageTextId)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MultiLanguageTextId)
#Html.ValidationMessageFor(model => model.MultiLanguageTextId)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Text)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Text)
#Html.ValidationMessageFor(model => model.Text)
</div>
</fieldset>
I'm testing with some pre-existing values and all the data is shown properly in the parent and in the child editors, when the page is shown. However, when I submit the form, all changes done inside any of the child editors are ignored.
My HttpPost method is this:
[HttpPost]
public ActionResult Edit(MultiLanguageText multilanguagetext)
{
if (ModelState.IsValid)
{
db.Entry(multilanguagetext).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(multilanguagetext);
}
My question is: How do I make it so that all values are posted, from both the parent model and its children model editors? (I've seen that other similar questions exist, but I've tried their solutions and they didn't seem to fix my problem.)
I'm testing your question. I think it's done and all values posted. But I think you need provide more information to fix your problem
After some trying, this is the solution that I found to work:
[HttpPost]
public ActionResult Edit(MultiLanguageText multilanguagetext)
{
if (ModelState.IsValid)
{
db.Entry(multilanguagetext).State = EntityState.Modified;
foreach (var local in multilanguagetext.LocalizedTexts) db.Entry(local).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(multilanguagetext);
}
I have a controller which looks like;
[HttpPost]
[Authorize(Roles = "Admin")]
public ActionResult ProjectAdd(PortfolioViewModel model, int[] categories, HttpPostedFileBase thumbnail, HttpPostedFileBase image)
{
model.ProjectImage = System.IO.Path.GetFileName(image.FileName);
model.ProjectThubmnail = System.IO.Path.GetFileName(thumbnail.FileName);
using (PortfolioManager pm = new PortfolioManager())
{
using (CategoryManager cm = new CategoryManager())
{
if (ModelState.IsValid)
{
bool status = pm.AddNewProject(model, categories);
}
ViewBag.Categories = cm.GetAllCategories();
ViewBag.ProjectsList = pm.GetAllProjects();
}
}
return View(model);
}
My View is;
#using (Html.BeginForm("projectAdd", "home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Add New Project</legend>
<div class="editor-label">
#Html.LabelFor(model => model.ProjectHeading)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ProjectHeading)
#Html.ValidationMessageFor(model => model.ProjectHeading)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ProjecctUrl)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ProjecctUrl)
#Html.ValidationMessageFor(model => model.ProjecctUrl)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ProjectLongDescription)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ProjectLongDescription)
#Html.ValidationMessageFor(model => model.ProjectLongDescription)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PromoFront)
</div>
#Html.EditorFor(model => model.PromoFront)
#Html.ValidationMessageFor(model => model.PromoFront)
<div class="editor-label">
<label for="thumbnail">Thumbnail</label>
</div>
<div class="editor-field">
<input type="file" name="thumbnail" id="thumbnail" />
</div>
<div class="editor-label">
<label for="image">Image</label>
</div>
<div class="editor-field">
<input type="file" name="image" id="image" />
</div>
<div class="editor-label">
<label for="categories">Categories</label>
</div>
#foreach (var c in categories)
{
<input type="checkbox" name="categories" value="#c.CategoryId">
#c.CategoryName
}
<p>
<input type="submit" value="Create" class="submit" />
</p>
</fieldset>
}
When I try this code, The ModeState.IsValid property becomes false (I saw through debugging). However, when I remove ModeState.IsValid, the insertation is done successfully and everything works exactly what I want.
I need ModeState.IsValid property for validating my view.
Updated: My viewmodel is;
[Key]
public int ProjectId { get; set; }
[Required(ErrorMessage="Please enter project heading")]
public string ProjectHeading { get; set; }
[Required(ErrorMessage = "Please enter project Url")]
public string ProjecctUrl { get; set; }
[Required(ErrorMessage = "Please enter project description")]
public string ProjectLongDescription { get; set; }
public string ProjectShortDescription
{
get
{
var text = ProjectLongDescription;
if (text.Length > ApplicationConfiguration.ProjectShortDescriptionLength)
{
text = text.Remove(ApplicationConfiguration.ProjectShortDescriptionLength);
text += "...";
}
return text;
}
}
public bool PromoFront { get; set; }
[Required(ErrorMessage = "You must sepcify a thumbnail")]
public string ProjectThubmnail { get; set; }
[Required(ErrorMessage = "You must select an image")]
public string ProjectImage { get; set; }
public int CategoryId { get; set; }
public IEnumerable<Category> Categories { get; set; }
Updated 2: I found the error. the problem is
{System.InvalidOperationException: The parameter conversion from type 'System.String' to type 'PortfolioMVC4.Models.Category' failed because no type converter can convert between these types.
at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.ValueProviderResult.ConvertTo(Type type, CultureInfo culture)
at System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary modelState, String modelStateKey, ValueProviderResult valueProviderResult, Type destinationType)}
When in debug, check the ModelState for errors. It's a key/value dictionary with all the properties required to make the model valid. If you check the Values-property you can find the value with the Errors-list that is not empty and see what the error is.
Or add this line of code in the action method to get all the errors for the model:
var errors = ModelState.Where(v => v.Value.Errors.Any());
You should rename your categories action parameter to something else because your PortfolioViewModel model already has a property called Categories which is of completely different type and which confuses the model binder:
[HttpPost]
[Authorize(Roles = "Admin")]
public ActionResult ProjectAdd(
PortfolioViewModel model,
int[] categoryIds,
HttpPostedFileBase thumbnail,
HttpPostedFileBase image
)
{
...
}
Now obviously you will also have to update your view to match the checkbox name.
While this might solve your issue I would very strongly recommend you using view models and stop passing your domain models to the views.