简体   繁体   English

通过视图将值从控制器传递到控制器

[英]Passing Value from controller to controller via View

I have two classes where one is a parent class for the other. 我有两个类,其中一个是另一个的父类。 The basic CRUD functions was created in the controller. 基本的CRUD功能是在控制器中创建的。 In the design of my table I have the parent id in my child class as the foreign key. 在表的设计中,我将子类中的父ID作为外键。 In the view for Create function of the child, I am asked to enter the parent ID. 在子级的“创建”功能视图中,要求我输入父级ID。 I have changed the Create to accept the ID of the parent. 我已经更改了创建以接受父代的ID。 But when I remove the code for selecting the parent id in the view I get exception in my Create. 但是,当我在视图中删除用于选择父ID的代码时,我的Create出现异常。 Is there a way I can set the parent ID in both my create functions(Over loaded functions). 有没有一种方法可以在两个创建函数(重载函数)中设置父ID。

    public ActionResult Create(int? id)
    {
        ViewBag.LsystemID = new SelectList(db.Lsystem, "LsystemID", "LsystemName",id);
        ViewBag.TCID = new SelectList(db.TC, "TCID", "TCName");
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "OptionID,OptionName,TCID,LsystemID")] Option option)
    {
        if (ModelState.IsValid)
        {
            db.Option.Add(option);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

    //    ViewBag.LsystemID = new SelectList(db.Lsystem, "LsystemID", "LsystemName", op);
        ViewBag.TCID = new SelectList(db.TC, "TCID", "TCName", option.TCID);
        return View(option);
    }

View 视图

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.LabelFor(model => model.OptionName, htmlAttributes: new { @class = "control-label col-md-2" })
    @Html.EditorFor(model => model.OptionName, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.OptionName, "", new { @class = "text-danger" })

    @Html.LabelFor(model => model.TCID, "TCID", htmlAttributes: new { @class = "control-label col-md-2" })
    @Html.DropDownList("TCID", null, htmlAttributes: new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.TCID, "", new { @class = "text-danger" })

    @Html.LabelFor(model => model.LsystemID, "LsystemID", htmlAttributes: new { @class = "control-label col-md-2" })
    @Html.DropDownList("LsystemID", null, htmlAttributes: new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.LsystemID, "", new { @class = "text-danger" })

    <input type="submit" value="Create" class="btn btn-default" /> 
}

How can I pass the value LsystemID without being shown in the View? 如何在不显示在视图中的情况下传递值LsystemID?

EDIT 1 : Adding Model class 编辑1:添加模型类

 public class Lsystem
{
    public int LsystemID { get; set; }
    public string LsystemName { get; set; }
    public virtual ICollection<Option> Options { get; set; }
  //  public int OptionId { get; set; }

}

public class Option
{
    public int OptionID { get; set; }
    public string OptionName { get; set; }
    public int TCID { get; set; }
    public virtual TC tc { get; set; }
    public virtual Lsystem Lsystem { get; set; }
    public int LsystemID { get; set; }
    public virtual ICollection<OptionValue> OptionValues { get; set; }
}

Start by creating a view model representing what you need in the view (add validation and display attributes as required 首先创建一个表示您在视图中需要的视图模型(根据需要添加验证和显示属性)

public class OptionVM
{
  public int Lsystem { get; set; }
  public string Name { get; set; }
  public int TC { get; set; }
  public SelectList TCList { get; set; }
}

and in the controller 并在控制器中

public ActionResult Create(int? id)
{
  OptionVM model = new OptionVM
  {
    Lsystem = id // set the parent
  };
  ConfigureViewModel(model);
  return View(model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(OptionVM model)
{
  if (!ModelState.IsValid)
  {
    ConfigureViewModel(model);
    return View(model);
  }
  Option option = new Option
  {
    OptionName = model.Name,
    TCID = model.TC,
    LsystemID= model.Lsystem
  };
  db.Option.Add(option);
  db.SaveChanges();
  return RedirectToAction("Index");
}

private void ConfigureViewModel(OptionVM model)
{
  model.TCList = new SelectList(db.TC, "TCID", "TCName");
}

and in the view 并认为

@model OptionVM
@using (Html.BeginForm())
{
  @Html.AntiForgeryToken()
  @Html.HiddenFor(m => m.Lsystem)

  @Html.LabelFor(m => m.Name)
  @Html.TextBoxFor(m => m.Name)
  @Html.ValidationMesageFor(m => m.Name)

  @Html.LabelFor(m => m.TC)
  @Html.DropDownListFor(m => m.TC, Model.TCList, "Please select")
  @Html.ValidationMesageFor(m => m.TC)

  <input type="submit" ... />
}

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

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