从表单MVC获取值

时间:2018-06-13 05:41:37

标签: asp.net-mvc

这是一个简单的问题,但我不知道如何解决这个问题       我设计了从数据库中获取数据的网页,我们可以编辑数据。但是如果我提交按钮以保存我所编辑的内容,则无法获取参数中的值。

图片参考 enter image description here

这是我的控制器

 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();
    }

这是我的viewmodel

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

这是我的观点

@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>
}

有人可以帮助我真的花了很多时间

1 个答案:

答案 0 :(得分:0)

你在使用2个不同模型的MovieViewModel传递给视图时提交你正在使用movieView

变化:

public ActionResult Submitform(MovieView movie)

要:

public ActionResult Submitform(MovieViewModel movie)