1

This is my combined model:

public class AddArticleModel
{
    public TBL_ARTICLES Article { get; set; }
    public IEnumerable<TBL_CATEGORIES> Categories { get; set; }
}

And controller that is use model.

public ActionResult AddArticle()
{
    AddArticleModel AddArticleModel = new AddArticleModel();
    AddArticleModel.Categories = entity.TBL_CATEGORIES.Select(a => a);

    return View(AddArticleModel);
}

And View :

@model DunyaYazilim.Models.AddArticleModel
@{
    ViewBag.Title = "AddArticle";
}
@using (Html.BeginForm((string)ViewBag.FormAction, "Home"))
{
    <fieldset>
        <legend>Add Article Form</legend>
        <ol>
            <li>
                @Html.DropDownListFor(m => m.Categories, Model.Categories.Select(c => new SelectListItem { Text = c.Name, Value = c.CategoryID.ToString() }), "-----Select Category----")
            </li>
        </ol>
        <input type="submit" value="Send" />
    </fieldset>
}

And Posted Method in controller:

[HttpPost]
public ActionResult AddArticle(AddArticleModel AddArticleModel)
{
   //Insert operations:
   return View(AddArticleModel);
}

My question: When I posted the form, Occur an error : Object reference not set to an instance of an object. In line:13

Line 12:             <li>
Line 13:                 @Html.DropDownListFor(m => m.Categories, Model.Categories.Select(c => new SelectListItem { Text = c.Name, Value = c.CategoryID.ToString() }), "-----Select Category----")
Line 14:             </li>
Line 15:             <li>

What the reason for this?

Note: There are a lot of examle in this site. I tried many of them, but I could not find reason of error.

Thanks.

AliRıza Adıyahşi
  • 14,706
  • 23
  • 108
  • 189
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – tom redfern Jan 11 '17 at 10:28

4 Answers4

5

I'm not sure if you posted the full code for your POST operation, but if you did then it's wrong.

When you post data to the server, you are not posting the values of your SelectList. You are only posting the selected value. If you are just displaying the view back to the user, then the SelectList will be null. You need to repopulate it in the Post:

[HttpPost]
public ActionResult AddArticle(AddArticleModel AddArticleModel)
{
    AddArticleModel.Categories = entity.TBL_CATEGORIES.Select(a => a);

    return View(AddArticleModel);
}

If you only want to return the selected category (seems odd) this will do it:

[HttpPost]
public ActionResult AddArticle(AddArticleModel AddArticleModel)
{
    AddArticleModel.Categories = entity.TBL_CATEGORIES.Where(a => a.CategoryId == AddArticleModel.CategoryId);

    return View(AddArticleModel);
}

This assumes that the property on your view model is CategoryId and the property on your TBL_CATEGORIES entity is also CategoryId.

Dismissile
  • 30,816
  • 34
  • 167
  • 253
  • AddArticleModel.Categories = entity.TBL_CATEGORIES.Select(a => a); is return all categories. I want to retrive only selected value. I m new for mvc sorry. – AliRıza Adıyahşi Apr 20 '12 at 14:09
  • AddArticleModel is not contain CategotyID. Syntax error is occured. – AliRıza Adıyahşi Apr 20 '12 at 14:32
  • I said in my answer that this is assuming you have a CategoryID. Since you have not posted your model I have no idea of knowing what it is. You're going to have to change it yourself. – Dismissile Apr 20 '12 at 16:18
1

I solved it

I used this:

<div>@Html.DropDownList("CategoryID", new SelectList(Model.Categories, "CategoryID", "Name"),"-----Select Category-----")</div>

and my controller:

[HttpPost]
public ActionResult AddArticle(AddArticleModel AddArticleModel,String CategoryID)
{
    TBL_ARTICLES article = AddArticleModel.Article;
    article.CategoryID = Int32.Parse(CategoryID);
    //some code...

    return View();
}

But still I don't know if it is the correct way.

Cœur
  • 32,421
  • 21
  • 173
  • 232
AliRıza Adıyahşi
  • 14,706
  • 23
  • 108
  • 189
0

Check the Model.Categories is not null in the condition.

@Html.DropDownListFor(m => m.Categories, Model.Categories != null ?               
       Model.Categories.Select
       (
        c => new SelectListItem 
        {
           Text = c.Name, 
           Value = c.CategoryID.ToString() 
        }) : null, "-----Select Category----")
VJAI
  • 29,899
  • 20
  • 93
  • 155
  • This prevents the Null Reference, but it doesn't really solve the problem. If he POSTs and redisplays the view using your code, there will be nothing in the dropdown and he wouldn't be able to change his selection at this point. – Dismissile Apr 20 '12 at 13:56
0

The problem is with this:

[HttpPost] 
public ActionResult AddArticle(AddArticleModel AddArticleModel) 
{ 
    //Insert operations: 
    return View(AddArticleModel); 
} 

When a form is submitted, only the selected item(s) of a dropdown are posted. So for example, if you have a dropdownlist with 100 options, if you select one option, when submitting the form, only that one option will be sent. As a result of that, the modelbinder doesn't have enough information to recreate your dropdownlist.

So you need to repopulate the dropdown before passing the model back to the view:

[HttpPost]  
public ActionResult AddArticle(AddArticleModel AddArticleModel)  
{  
    AddArticleModel.Categories = entity.TBL_CATEGORIES.Select(a => a);  

    return View(AddArticleModel);  
}  

You can check for this yourself by using Fiddler2.

John H
  • 13,157
  • 4
  • 33
  • 68