简体   繁体   English

如何在MVC中使用单独的模型类进行验证

[英]How to use a separate model class for validation in MVC

I am not sure on how to implement this, I have a MovieController.cs in the Controllers folder and a MovieCreateViewModel.cs in the Models folder. 我不确定如何实现,我在Controllers文件夹中有MovieController.cs,在Models文件夹中有MovieCreateViewModel.cs。 I need to add validation for the create, edit and delete views. 我需要为创建,编辑和删除视图添加验证。

MovieDetailsViewModel.cs MovieDetailsViewModel.cs

public class MovieDetailsViewModel
{
    public int Id { get; set; }
 }     

then I have MovieController.cs 然后我有MovieController.cs

public class MovieController : Controller
{

    Connect connection;
    MovieCreateViewModel movie;
    MovieDetailsViewModel id;

    public MovieController()
    {
        this.connection = new Connect();
        this.movie = new MovieCreateViewModel();
        this.id = new MovieDetailsViewMode();
    } 

    public ActionResult Edit(MovieDetailsViewModel id)
    {
        movie = this.connection.MovieContext.Where(m => m.ID == id).SingleOrDefault();  **//I get an error here**
        return View(movie);
    }

    //
    // POST: /Movie/Edit/5

    [HttpPost]
    public ActionResult Edit(MovieCreateViewModel movieedit)
    {
        try
        {
            if (ModelState.IsValid)
            {
                this.connection.MovieContext.AddObject(movieedit);
                this.connection.MovieContext.Context.SaveChanges();
                return RedirectToAction("Index");
            }

        }
        catch
        {
            return View(movieedit);
        }
    }

for the httpPost I made the type MovieDetailsViewModel id in the parameter list Where do I go from here please? 对于httpPost,我在参数列表中输入了MovieDetailsViewModel id类型, MovieDetailsViewModel id从这里我应该去哪里?

Thanks 谢谢

Validation will now be performed on your model instance, to check it, you do: 现在将在模型实例上执行验证,要进行验证,请执行以下操作:

ModelState.IsValid

In the controller method prior to the save operation. 在控制器方法之前进行保存操作。 As long as the input names on the view correspond with your model class's property names, binding and validation will be performed implicitly prior to your action method being executed. 只要视图上的输入名称与模型类的属性名称相对应,就将在执行action方法之前隐式执行绑定和验证。 To show your validation messages in your view, add a Html.ValidationMessage() to the top of the view. 要在视图中显示验证消息,请在视图顶部添加Html.ValidationMessage()。 Hope this helps. 希望这可以帮助。 By the way its well worth checking out Foolproof Validation which provides conditional validation attributes and some other good stuff. 顺便说一句,值得一提的是Foolproof Validation ,它提供了条件验证属性和其他一些好东西。

Better practice would be use seperate model and viewmodel. 更好的做法是使用单独的模型和视图模型。 Convert your model to viewmodel and then pass it to view 将模型转换为viewmodel,然后将其传递给view

public ActionResult Edit(int id)
{
     var movie = this.connection.MovieContext.SingleOrDefault(m => m.ID == id);
     var vm = new MovieCreateViewModel{ Id = movie.Id};
     return View(vm);
}

//
// POST: /Movie/Edit/5

[HttpPost]
public ActionResult Edit(MovieCreateViewModel vm)
{
    try
    {
        if (ModelState.IsValid)
        {
            var movie = new Movie{Id = vm.Id};
            this.connection.MovieContext.Attach(movie);
            this.connection.MovieContext.Context.SaveChanges();
            return RedirectToAction("Index");
        }

    }
    catch
    {
        return View(movieedit);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM