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

How to access specific rows and insert values into specific columns of a table?

我有一个基本模型:

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

我想将两个不同的控制器连接到这个单一视图,一个用于只能填写标题和金额字段的请求者。 财务经理的第二个控制器通过填写 table 中的 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>
 }

现在基于两个控制器的 razor 视图设计,请求者可以插入两个预定义字段,即标题和金额,财务经理也是如此,只有 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>

您的 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.
}

财务经理对上述id记录的Finance_Approval字段进行更改后为10

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

然后你需要像

一样更新上面的记录
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!");
}