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

问题描述 投票:1回答:1

我有一个基本模型:

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 Manger的第二个控制器通过填写表中的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字段。

但问题是,每当财务经理批准来自其门户网站的请求时,可以将某些值称为“是”,然后将此值插入到一个全新的行中,该行与请求者填充的行没有任何关联。

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


@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>

c# asp.net-mvc entity-framework ef-code-first
1个回答
0
投票

你的Requester Controller和它的View是完美的意味着请求者成功地向db添加了新的记录。

例如: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!");
}
© www.soinside.com 2019 - 2024. All rights reserved.