-2

This the simple question but i don't know how to over come this issue I have design the webpage which get the data from the database and we can able to edit the data. But if i submit the button for save what ever i have edited it does not getting the value in the parameter.

image reference enter image description here

And this is my controller

 public ActionResult Movielist()
    {
        var movie = _context.MovieviewTable.ToList();
        ViewBag.movielist = movie;

        return View();
    }

    public ActionResult tabelForm()
    {
        var dropdown = _context.MovieviewTable.ToList();
        var viewmodel = new MovieViewModel
        {
            MovieDropdownCollection=dropdown
        };

        return View(viewmodel);
    }

    public ActionResult Edit(int id)
    {
        var refelect = _context.MovieviewTable.SingleOrDefault(c => c.id == id);
        var viewmodel = new MovieViewModel
        {
            movieView = refelect,
            MovieDropdownCollection = _context.MovieviewTable.ToList()
    };
        return View("tabelForm", viewmodel);
    }
    [HttpPost]
    public ActionResult Submitform(MovieView movie)
    {
        //if(movieViewModel.Movieviewdatacollection.id==0)
        //{
        //    _context.MovieviewTable.Add();
        //}

        return View();
    }

And this is my viewmodel

public IEnumerable<MovieView> MovieDropdownCollection
    {
        get;set;
    }
    public MovieView movieView
    {
        get;set;
    }

And this is my View

@model Practiceupdateedit.ViewModel.MovieViewModel
@{
    ViewBag.Title = "tabelForm";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>tabelForm</h2>

@using (Html.BeginForm("Submitform", "Movie"))
{
    <div class="form-group">
        @Html.LabelFor(m => m.movieView.MovieName)
        @Html.TextBoxFor(m => m.movieView.MovieName, new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.movieView.AddedOn)
        @Html.TextBoxFor(m => m.movieView.AddedOn, new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.movieView.Genere)
        @Html.DropDownListFor(m => m.movieView.Genere,new SelectList(Model.MovieDropdownCollection,"Genere","Genere"), new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.movieView.Instock)
        @Html.TextBoxFor(m => m.movieView.Instock, new { @class = "form-control" })
    </div>
    <button class="btn btn-primary">Save</button>
}

Can someone plz help me really i have spend to-much of time

Aravindhan R
  • 270
  • 5
  • 26
  • View models do NOT contain data models. Refer [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Jun 13 '18 at 06:25
  • And your view is based on `MovieViewModel` therefore the parameter in the POST method also needs to be `MovieViewModel`, not `MovieView` –  Jun 13 '18 at 06:26
  • ya correct but if add the `MovieviewModel` it showing error at while adding movie ` _context.MovieviewTable.Add(movie);` and my `MovieViewTable` is my dbset @StephenMuecke – Aravindhan R Jun 13 '18 at 06:30
  • Read the link I gave you. You get the data model from the db and map the view model properties to it, then save the data model –  Jun 13 '18 at 06:31
  • Thanks @StephenMuecke if i use movieviewmodel then it showing cannot convert viewmodel to model while add `_context.MovieviewTable.Add(movie)` any solution is there for this? – Aravindhan R Jun 13 '18 at 06:41
  • READ THE LINK I GAVE YOU!!! –  Jun 13 '18 at 06:42

1 Answers1

0

you are using 2 different model MovieViewModel while passing to the view and while submiting you are using movieView

Change:

public ActionResult Submitform(MovieView movie)

To:

public ActionResult Submitform(MovieViewModel movie)
Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
  • if i use movieviewmodel i throwing error like cannot convert from viewmodel.movieviewmodel to models.moviewview if(movieViewModel.Movieviewdatacollection.id==0) { _context.MovieviewTable.Add(); } – Aravindhan R Jun 13 '18 at 05:51
  • I think so ou should be able to – Arijit Mukherjee Jun 13 '18 at 05:54
  • @Aeijit Mukherjee if i use movieviewmodel it throwing error like cannot convert from viewmodel.movieviewmodel to models.moviewview if(movieViewModel.Movieviewdatacollection.id==0) { _context.MovieviewTable.Add(); } while adding in context.movieviewtable.add(movie) – Aravindhan R Jun 13 '18 at 06:01