-1

I have 3 models names images,game and imageviewmodel

public class game
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]

    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    public virtual ICollection<images> Image { get; set; }

}
public class images
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]

    public int Id { get; set; }
    [Required]
    [DataType(DataType.ImageUrl)]
    public string Image1Url { get; set; }
    public virtual game Game { get; set; }

}
public class ImageViewModel
{
    [Required]
    [DataType(DataType.Upload)]
    public HttpPostedFileBase ImageUpload { get; set; }
    public virtual game Game { get; set; }

}
public class GameDb : DbContext
{
    public DbSet<game> Games { get; set; }
    public DbSet<images> Images { get; set; }
 }

My view is strongly typed view of imageviewmodel . I have a dropdown list there with all games filled Here is my GET create method

public ActionResult Create()
        {
            ViewBag.GameId = new SelectList(db.Games, "Id", "Name");
            return View(new ImageViewModel());
        }

my dropdown is filled with GenreId

 <div class="editor-field">
            @Html.DropDownList("GameId","Select an Item")
        </div

On my POST create method I want to access dropdown value id to insert in image table

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ImageViewModel model)
         {
         }

I am unable to access game id of drop down list.I am doing like this

var img=new images{

Game.( no intellisense) =model.Game.id,

};

How do I resolve that need some help.

tereško
  • 58,060
  • 25
  • 98
  • 150
Ghazanfar Khan
  • 3,648
  • 8
  • 44
  • 89

1 Answers1

0

First of all consider follow the naming convensions when you name your classes.

Second, consider using DropDownListFor helper method instead of DropDownList

And finally you have to create new Game object instance before set it's id:

var img = new images
{
   Game = new game { Id = model.Game.Id }
};
Max Brodin
  • 3,903
  • 1
  • 14
  • 23
  • Object reference not set to an instance of an object. it gives this error on your code – Ghazanfar Khan Dec 07 '14 at 19:05
  • Seems `Game` object of your model is `null`. So your binding doesn't work, try to use `DropDownListFor` binding, you can find simple example in this [SO question](http://stackoverflow.com/questions/7142961/mvc3-dropdownlistfor-a-simple-example) – Max Brodin Dec 07 '14 at 19:06
  • BTW do you really need `Game` object in your `ImageViewModel`? You should replace it with simple `GameId` property – Max Brodin Dec 07 '14 at 19:10
  • How do I get that id filled with what I have selected in drop down? – Ghazanfar Khan Dec 07 '14 at 19:12
  • I am uploading image and want to insert ID of game in image table – Ghazanfar Khan Dec 07 '14 at 19:13
  • 1
    1. You don't want to save `HttpPostedFileBase` in the database, you need to save file to the hard drive and store path to it in DB as `string`. – Max Brodin Dec 07 '14 at 19:18
  • Yes I dont post that code here it is saving in a folder but how to get id of game selected in dropdown list. I am using this link for upload image http://cpratt.co/file-uploads-in-asp-net-mvc-with-view-models/ – Ghazanfar Khan Dec 07 '14 at 19:19
  • You should have `GameId` in your `ImageViewModel` and create new `game` like `var img = new images { Game = new game { Id = model.GameId, ImageUrl = 'path to the image' } };` – Max Brodin Dec 07 '14 at 19:22
  • What to do with Name field of game? – Ghazanfar Khan Dec 07 '14 at 19:24
  • Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. – Ghazanfar Khan Dec 07 '14 at 19:30