MVC CRUD操作。 [英] MVC CRUD operations.

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

问题描述

我有以下SQL表和控制器:



公司

ID int PRIMARY KEY,< br $>
CompanyName varchar(100),

CompanyEmail varchar(100),

CompanyPass varchar(100)



分支

ID int PRIMARY KEY,

BranchLocation varchar(100),

经理varchar(100),

CompanyName varchar(100)外国参考公司公司(公司名称)



公共ActionResult regCompany()

{}



public ActionResult regBranch()

{}



如何在regBranch View上显示CompanyName,并将该值与BranchLocation和Manager一起保存到Branch表中?

I have the following SQL tables and controllers:

Company
ID int PRIMARY KEY,
CompanyName varchar(100),
CompanyEmail varchar(100),
CompanyPass varchar(100)

Branch
ID int PRIMARY KEY,
BranchLocation varchar(100),
Manager varchar(100),
CompanyName varchar(100) FOREIGN KEY REFERENCES Company(CompanyName)

public ActionResult regCompany()
{}

public ActionResult regBranch()
{}

How do I display the CompanyName on the regBranch View and also save that value to the Branch table together with the BranchLocation and Manager?

推荐答案

这是你的内容需要这样做。

1.创建一个模型而不是使用视图包

Here is what you need to do.
1. create a model instead of using view bag
class MyModel
{
  public string CompanyName {get; set;}
  public string CompanyEmail {get; set;}
  public string CompanyPass {get; set;}
  public string BranchLocation {get; set;}
  public string BranchManager {get; set;}
}



2.设置您的视图以使用此模型


2. set your view to use this model

@model MyModel



3.在控制器内设置模型字段


3. set your model fields inside your controller

public class MyController : Controller
{
  MyModel myModelObj;
  public ActionResult regCompany()
  {
    myModelObj = new MyModel();
    myModelObj.CompanyName = "Some company";
    .....
    .....
    return RedirectToAction("regBranch", "SetupQueue", new {companyName = CompanyName ...}
  }

  public ActionResult regBranch(string CompanyName....)
  {
      var companyName = CompanyName;
  }
}



如果您尝试在两者之间传递值,这是如何完成的一般概念两个控制器。如果你需要在视图中显示模型然后将值传递给控制器​​,控制器应该返回View对象而不是RedirectToAction。所以你会这样做


This is the general idea of how it should be done if you are trying to pass values between two controllers. If you need to display the model inside the view and then pass values to your controller the the controller should return the View object and not RedirectToAction. So you would do this

public ActionResult regCompany(){
return View(myModelObj);}



并在视图中


and inside the view

@Html.TextBoxFor(model => model.BranchManager)



最后是将接收值的控制器形成视图


and finally the controller that will receive the values form the view

regBranch(string BranchManager....)
{
  var barnchManager = BranchManager
}


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

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