0

I have page, which allows me to add interesting places (POI), based on the different predifined categories:

Models

public class POI
{
    public long Id { get; set; }
    public string TitleNor { get; set; }
    public string TitleEng { get; set; }
    public Category Category { get; set; }
    public string DescriptionNor { get; set; }
    public string DescriptionEng { get; set; }
    public float Latitude { get; set; }
    public float Longitude { get; set; }
    public string ImagePath { get; set; }
    public HttpPostedFileBase UploadedPhoto { get; set; }
}

public class Category
{  
    public int Id { get; set; }
    public string TitleNor { get; set; }
    public string TitleEng { get; set; }
}

Create

@Html.DropDownListFor(model=>model.Category.Id, ViewBag.Categories as SelectList, "", new { @class = "form-control" })

Controller

List<Category> categories = new List<Category>();
        var categoryQry = from d in db.Categories
                          select d;
        categories.AddRange(categoryQry.Distinct());
        ViewBag.Categories = new SelectList(categories, "Id", "TitleNor");
        return View(pOI);

So when I create a new POI, I get the following state of POI: state POI

TitleEng and TitleNor = null. So if I will add a new POI object, the Category object, only with the Id, will be added as well.

So Category is not mapped as a property, only it's ID filed is. As I understand one of the solutions is to change POI model and instead of property Category have only CategoryID. But how can I get a proper mapping, if I don't want to change a model? Thanks in advance.

11
  • Is your category contain the same column name as model ? Commented Sep 9, 2014 at 9:13
  • @KrishnrajRana If you mean "TitleNor" and "TitleEng" - then yes. Both category and POI have such properties. Can it be a problem for mapping? Commented Sep 9, 2014 at 9:16
  • Yes i guess, check any spell mistake and if it is correct then specify column name in your categoryQry linq query. Commented Sep 9, 2014 at 9:21
  • Do you mean this is happening on postback? Commented Sep 9, 2014 at 9:28
  • 1
    You cant use model=>model.Category because model.category is a complex object. A <select> control only posts back a single value. If you need the category fully populated, then you have to get it again from the database. Commented Sep 9, 2014 at 10:55

1 Answer 1

1

On post back you only get the values you send to the controller. You only send Category.Id. (from the dropdown), but you do not have inputs for Category.TitleNor and Category.TitleEng so they will be null.

You cannot use @Html.DropDownListFor(model=>model.Category, ViewBag.Categories as SelectList ... because Category is a complex object and a control only posts back a single value.

Ideally you should use a view model that contains property public int CategoryID { get; set; } for binding to the dropdown.

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

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.