0

As I am newbie to asp.net mvc 3, I have no good knowledge of it's internal process. I have two action to create new category as Get and Post method:

   public ActionResult Create()
        {
            List<Category> cat = this.display_children(null, 0);
            List<SelectListItem> items = new SelectList(cat, "ID", "CategoryName").ToList();
            items.Insert(0, (new SelectListItem { Text = "root", Value = "" }));
            ViewBag.ParentCategoryID = items;
            return View();
        } 

        [HttpPost]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.AddObject(category);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            List<Category> cat = this.display_children(null, 0);
            List<SelectListItem> items = new SelectList(cat, "ID", "CategoryName").ToList();
            items.Insert(0, (new SelectListItem { Text = "root", Value = "" }));
            ViewBag.ParentCategoryID = items;
            return View(category);
        }

Below is View:

@model IVRControlPanel.Models.Category

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Category</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.CategoryName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CategoryName)
            @Html.ValidationMessageFor(model => model.CategoryName)
        </div>

       <div class="editor-label">
           @Html.Label("Select Category")
        </div>
      <div>
          @*@Html.DropDownList("CategoryList",new SelectList(ViewBag.Categories))*@
          @Html.DropDownList("ParentCategoryID", ViewBag.ParentCategoryID as SelectList) 
          @Html.ValidationMessageFor(model => model.ParentCategoryID)

        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Problem: When I click the create button without filling up the category-name field following exception is thrown:

This property cannot be set to a null value.

The exception is thrown only when visual studio is debugging mode and when I continue debugging then only error is shown in validation message. Here, What actually have to be is that Error should be shown without throwing exception which is alright while not in debugging mode. I have following category table column in database and use model first approach Entity framework:

  • ID -> primary key and identity , integer
  • CategoryName -> Non nullable, varchar(50)
  • ParentCategoryID -> Nullable

I have not good at asp.net mvc 3 and can not figured what might be the problems.

CodeManiac
  • 974
  • 8
  • 34
  • 56

2 Answers2

0

In your actions replace:

ViewBag.ParentCategoryID = items;

with:

ViewBag.Categories = items;

and in your view:

@Html.DropDownListFor(x => x.ParentCategoryID, ViewBag.Categories as SelectList) 

The DropDownList helper needs 2 arguments: the first one represents a scalar property that will hold the selected value and the second argument a collection with the available items. They should be different.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I have done what you have told but showing `Compiler Error Message: CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type`. Here, ParentCategoryID is integer type. Is there other way – CodeManiac Jul 12 '12 at 06:18
  • Thanks for your answer. Finally I have got solution. Problem was that CategoryName string was converted to null due to prebinding validation so, I have added attributes DisplayFormat(ConvertEmptyStringToNull = false). Now, I exception is not thrown and also showing the client validation message – CodeManiac Jul 12 '12 at 06:41
0

Luckily I found solution for this. Actually, Problem was due to PreBinding validation. I was searching and found same issue at this link explained nicely.

I have made partial class for Category as below:

  public partial class Category{
    }

    public class TestEntityValidation{
        [Required]
        [DisplayFormat(ConvertEmptyStringToNull = false)]
        public String CategoryName { get; set; }
    }

Here, Converting empty string to null is set to false which have solved my problem at DisplayFormat attributes.

Community
  • 1
  • 1
CodeManiac
  • 974
  • 8
  • 34
  • 56