Home > Net >  Adding a model's data to another model in Post-Action .NET MVC
Adding a model's data to another model in Post-Action .NET MVC

Time:06-04

all. This is my very first project on .NET MVC. I'm facing a problem with my post action. All I want to do is GET the data from model A and post it back to model B. But When I press the button, all values returns null to database. Here is my code. (I'm very unexperienced for coding please bear with me.)

Model Movie

 public class Movie
    {
        public int MovieID{ get; set; }
        public string MovieAdi{ get; set; }
        public string MovieAciklama{ get; set; }
        public int TurID { get; set; }
        public int TemaID { get; set; }
        public int PlatformID { get; set; }
    }

Model Suggestion

    public class Suggestion
    {     
        public int Id { get; set; }
        public string MovieAdi{ get; set; }
        public string MovieAciklama{ get; set; }
        public int TurID { get; set; }
        public int TemaID { get; set; }
        public int PlatformID { get; set; }
    }

My Controller

  public ActionResult SuggestionList()
        {
            var model = db.Suggestion;
            return View(model.ToList());
        }

        [HttpPost]
        public ActionResult SuggestionList(Suggestion model)
        {
            
            Movie pmovie= new Movie();

            pmovie.MovieAdi= model.MovieAdi;
            pmovie.TurID = model.TurID;
            pmovie.TemaID = model.TemaID;
            pmovie.PlatformID = model.PlatformID;
            

            db.Movie.Add(pmovie);
            db.SaveChanges();
 
            return RedirectToAction("Index"); //index.cshtml is where i show all of the movies.

        }

I use Displayfor and HiddenFor at the same time. Here is the SuggestionList.cshtml @model IEnumerable<NProject.Models.Suggestion>

 @using (Html.BeginForm("SuggestionList", "Panel", FormMethod.Post))
    {
        <table style="position:absolute; left:35%; top:30%;">

            <tr>
                <th>
                    <label>MovieID</label>
                </th>
                <th>
                    <label>MovieAdi</label>
                </th>
                <th>
                    <label>PlatformID</label>
                </th>
                <th>
                    <label>TemaID</label>
                </th>
                <th>
                    <label>TurID</label>
                </th>
                <th>
                    <label>Add it.</label>
                </th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td style="padding:20px;">
                        @Html.HiddenFor(modelItem => item.Id)
                        @Html.DisplayFor(modelItem => item.Id)
                    </td>
                    <td>
                        @Html.HiddenFor(modelItem => item.MovieAdi)
                        @Html.DisplayFor(modelItem => item.MovieAdi)
                    </td>
                    <td>
                        @Html.HiddenFor(modelItem => item.PlatformID)
                        @Html.DisplayFor(modelItem => item.PlatformID)
                    </td>
                    <td>
                        @Html.HiddenFor(modelItem => item.TemaID)
                        @Html.DisplayFor(modelItem => item.TemaID)
                    </td>
                    <td>
                        @Html.HiddenFor(modelItem => item.TurID)
                        @Html.DisplayFor(modelItem => item.TurID)
                    </td>
                    <td>        
                        <button >Add the suggestion.</button>
                    </td>

                </tr>
            }

        </table>

    }

CodePudding user response:

Firstly, each row in the view must have its own form.

Secondly @Html.HiddenFor(model => item.Id) model value becomes "item.Id" but your model properties are "Id","Name"... not item."xx". So you should use like @Html.Hidden("Id",value)

@foreach(var item in Model){
  @using (Html.BeginForm("SuggestionList", "Panel", FormMethod.Post)){
      <tr>
         <td style="padding:20px;">
             @Html.Hidden("Id",item.Id)
             @Html.DisplayFor(modelItem => item.Id)
         </td>
         <td>
            @Html.Hidden("MovieAdi",item.MovieAdi)
            @Html.DisplayFor(modelItem => item.MovieAdi)
         </td>
          .....
          .....
          .....
        <button >Submit</button>

      }
    }
  • Related