ASP.NET MVC单信息查看CRUD操作 [英] ASP.NET MVC Single View for CRUD operation

查看:144
本文介绍了ASP.NET MVC单信息查看CRUD操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP.NET MVC项目。我有一个关于在CRUD操作视图的问题。

在大多数我所看到的,每个CRUD操作独立意见的例子(如添加,编辑,删除)被使用。现在想象一下,如果我在我的数据库100表,并通过查看他们每个人都需要CRUD操作。它是最好创建为每个表这些单独的意见或创建,将创建这些意见对我来说,如下面的函数?

 公众的ActionResult CreateSubject()
{
    返回查看(Model.toList());
}公众的ActionResult EditSubject()
{
    返回查看();
} 公众的ActionResult DeleteSubject()
{
    返回查看();
}


解决方案

我用我的控制器上为每个操作单独行动,并创建一个简单的PartialView处理所有的字段,然后我使用我的共享文件夹的共享视图的负载我的局部视图。

 公共类CRUDController:控制器{
  公众的ActionResult的Create(){
    返回查看(新CreateModel());
  }  的[AcceptVerbs(HttpVerbs.Post)
  公众的ActionResult创建(CreateModel模型){
    如果(ModelState.IsValid){
      //保存到数据库返回查看或重定向
    }    返回查看(模型);
  }  公众的ActionResult编辑(它的id){
    VAR模型=新EditModel();
    模型= //负载和数据库地图
    返回查看(模型);
  }  的[AcceptVerbs(HttpVerbs.Post)
  公众的ActionResult创建(EditModel模型){
    如果(ModelState.IsValid){
      //保存到数据库返回查看或重定向
    }    返回查看(模型);
  }
}

支持接口:

 公共接口ICreateModel {
}
公共接口IEditModel {
  INT标识{获取;设置;}
}

CreateModel.cs:

 公共类CreateModel:ICreateModel {
  公共字符串SomeProp {获取;设置;}
}

EditModel.cs:

 公共类EditModel:CreateModel,IEditModel {
  公众诠释标识{获取;设置;}
}

_create.cshtml:

  @model CreateModel
@ Html.TextBoxFor(X => x.SomeProp)

Create.cshtml:

  @model ICreateModel
@using(Html.BeginForm){
  @ Html.Partial(_制造)
  <输入类型=提交值=提交/>
}

Edit.cshtml

  @model EditModel
@using(Html.BeginForm){
  @ Html.Partial(_制造)
  @ Html.HiddenFor(X => x.Id)
  <输入类型=提交值=提交/>
}

这是我如何处理我的多CRUD操作(至少在展示的形式对他们而言)的例子。这里显然是更多的内容在创建/编辑观点

通过将Edit.cshtml和Create.cshtml在共享文件夹,它会被默认,当您从这些名字的动作返回一个视图中使用。默认情况下,视图引擎检查控制器的文件中的相应视图文件夹,然后查找到共享。您的每一个部分_create.cshtml的,应在适当命名的查看文件夹,他们将被正确地传递。

I am working on a ASP.NET MVC project. I have a question about the View in a CRUD operation.

In most of the examples I have seen, separate views for each CRUD operation (e.g. Add, Edit, Delete) are used. Now imagine if I have 100 tables in my database, and each of them requires CRUD operations via a View. Is it best to create these separate Views for each table or create a function that would create these Views for me, such as below?

public ActionResult CreateSubject()
{
    return View(Model.toList());
}

public ActionResult EditSubject()
{
    return View();
}

 public ActionResult DeleteSubject()
{
    return View();
}

解决方案

I use separate actions for each operation on my controller and create a simple PartialView that handles all the fields and then I use a shared View from my Shared folder that loads my partial view.

public class CRUDController : Controller {
  public ActionResult Create() {
    return View(new CreateModel());
  }

  [AcceptVerbs(HttpVerbs.Post)]
  public ActionResult Create(CreateModel model) {
    if(ModelState.IsValid) {
      //save to db return view or redirect
    }

    return View(model);
  }

  public ActionResult Edit(it id) {
    var model = new EditModel();
    model = //load and map from db    
    return View(model);
  }

  [AcceptVerbs(HttpVerbs.Post)]
  public ActionResult Create(EditModel model) {
    if(ModelState.IsValid) {
      //save to db return view or redirect
    }

    return View(model);
  }
}

Supporting interfaces:

public interface ICreateModel {
}


public interface IEditModel {
  int Id {get;set;}
}

CreateModel.cs:

public class CreateModel : ICreateModel {
  public string SomeProp {get;set;}
}

EditModel.cs:

public class EditModel : CreateModel, IEditModel {
  public int Id {get;set;}
}

_create.cshtml:

@model CreateModel
@Html.TextBoxFor(x=>x.SomeProp)

Create.cshtml:

@model ICreateModel    
@using(Html.BeginForm) {
  @Html.Partial("_create")
  <input type="submit" value="Submit"/>
}

Edit.cshtml

@model EditModel
@using(Html.BeginForm) {
  @Html.Partial("_create")
  @Html.HiddenFor(x=>x.Id)
  <input type="submit" value="Submit"/>
}

This is an example of how I handle my multiple CRUD operations (at least in terms of showing forms for them). There would obviously be more content in your Create/Edit views

By placing the Edit.cshtml and Create.cshtml in the Shared folder, it'll be used by default when you return a view from an action with those names. By default the view engine checks the appropriate view folder for the controller for the file and then looks to Shared. Each of your _create.cshtml partial should be in the appropriately named View folders and they will be delivered correctly.

这篇关于ASP.NET MVC单信息查看CRUD操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆