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.

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
AddArticleModel is not contain CategotyID. Syntax error is occured.
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.
2

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.

Comments

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----")

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.