如何访问特定行并将值插入表的特定列?

时间:2018-10-31 13:49:34

标签: c# asp.net-mvc entity-framework ef-code-first

我有一个基本模型:

public class Events
{
    public int Id { get; set; }
    public string title { get; set; }
    public string amount { get; set; }
    public string Finance_Approval { get; set; }
}

我想将两个不同的控制器连接到该单一视图,一个用于只能填写标题和金额字段的请求者。 财务经理的第二个控制器通过填写表中的Finance_Approval字段来批准请求。

我创建了两个控制器及其剃刀视图,如下所示:

请求者控制器:

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

    public ActionResult Request(Events e)
    {
        _context.evt.Add(e);
        _context.SaveChanges();

        return Content("Added");
    }

这是剃刀视图:

@using (Html.BeginForm("Request", "Requester"))
{
<div class="form-group">
    @Html.LabelFor(a => a.title)
    @Html.TextBoxFor(a => a.title, new { @class = "form-control" })
</div>
<div class="form-group">
    @Html.LabelFor(a => a.amount)
    @Html.TextBoxFor(a => a.amount, new { @class = "form-control" })
</div>

<button class="btn btn-primary">Request</button>
}

这是财务总监:

  public ActionResult Index()
    {

        return View();
    }

    public ActionResult Approve(Events e)
    {

        _context.evt.Add(e);
        _context.SaveChanges();
        return Content("Approved!");
    }

这是其剃刀视图:

@using (Html.BeginForm("Approve", "Finance"))
{

<div class="form-group">
    @Html.LabelFor(a => a.Finance_Approval)
    @Html.TextBoxFor(a => a.Finance_Approval, new { @class = "form-control" })
</div>

<button class="btn btn-primary">Approve</button>
 }

现在,基于两个控制器的剃刀视图设计,请求者可以将其插入到两个预定义字段中,即标题和金额,财务管理器也将插入至仅Finance_Approval字段。

但是问题是,每当财务经理批准来自其门户的请求时,都说“ Yes”(是),然后将该值插入一个全新的行中,该行与该行填充的行没有任何关系。请求者。

我希望财务经理应该能够批准具有某些值的行(由请求者填充),但是我无法弄清楚逻辑。


@erShoaib这是最终更新:

  public ActionResult Index()
    {

        var all = _context.evt.ToList();
        return View(all);

    }

    public ActionResult Details(int? Id)
    {
        if (Id == 0)
        {
            return HttpNotFound();
        }

        var evtt = _context.evt.Find(Id);

        return View(evtt);
    }

    public ActionResult Approve(Events e)
    {
        if (e==null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Events evt = _context.evt.Find(e.Id);
        evt.Finance_Approval = e.Finance_Approval;

        //_context.evt.Add(e);
        _context.SaveChanges();
        return Content("Approved!");
    }

这是完整索引:

   @model IEnumerable<InsertFromdifferentControllers.Models.Events>

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<table class="table table-bordered table-hover">
@foreach (var item in Model)
{

  <tbody>
      <tr>
          <td>@item.Id</td>
          <td>@item.title</td>
          <td>@item.amount</td>
          <td>@Html.ActionLink("Expand","Details","Finance", new { Id = 
  @item.Id},null)</td>
      </tr>
  </tbody>

  }

 </table>

这是“详细信息”视图:

@model InsertFromdifferentControllers.Models.Events

<table class="table table-bordered table-hover table-responsive">


<tbody>
    <tr>
        <td>@Model.Id</td>
        <td>@Model.title</td>
        <td>@Model.amount</td>
        @using (Html.BeginForm("Approve","Finance"))
        {
            <div class="form-group">
                @Html.LabelFor(a=> a.Finance_Approval);
                @Html.TextBoxFor(a=> a.Finance_Approval, new { @class="form-control"})
            </div>
            <button class="btn btn-primary">Save</button>
        }
    </tr>
</tbody>

1 个答案:

答案 0 :(得分:0)

您的Requester Controller及其View非常完美,意味着请求者已成功向数据库添加了新记录。

例如:title = "Abc"amount="123",并在添加此记录后,假设id10

现在在Finance Controller中,您需要在Index方法中获得高于记录的记录,例如

public ActionResult Index(int id=0)                 <=  Pass "id=10"
{
   if(id == 0)
   {
     return HttpNotFound();
     //or code to handle request when id = 0
   }

   Events events = _context.evt.Find(id);           <= The "Find" function can get record with id=10
   return View(events);                             <= Pass found record to view.
}

财务经理使用Finance_Approval对上述记录的id字段进行了更改后,就是10

例如:Finance_Approval = "Approved"。然后,具有此新字段的所有记录字段都可以发布到Approve中的Finance Controller操作方法中。

然后您需要更新上述记录,例如

public ActionResult Approve(Events e)               <= Now "e" contains id=10, title="Abc", amount="123" and Finance_Approval="Approved"
{
    if(e == null)
    {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    Events events = _context.evt.Find(e.id);        <= Fetch record of id=10 from database.
    events.Finance_Approval = e.Finance_Approval;   <= Assign changes to found record with posted data from view.
    _context.SaveChanges();
    return Content("Approved!");
}