问题
I have a basic Model as:
public class Events
{
public int Id { get; set; }
public string title { get; set; }
public string amount { get; set; }
public string Finance_Approval { get; set; }
}
I want to connect two different controllers to this single view, one for the requesters who can fill only the title and amount fields. And the second controller for Finance Manger to approve the request by filling the Finance_Approval field in the table.
I have created two controllers and their razor views as follows:
Requester Controller:
public ActionResult Index()
{
return View();
}
public ActionResult Request(Events e)
{
_context.evt.Add(e);
_context.SaveChanges();
return Content("Added");
}
It's Razor View:
@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>
}
And this is the Finance Controller:
public ActionResult Index()
{
return View();
}
public ActionResult Approve(Events e)
{
_context.evt.Add(e);
_context.SaveChanges();
return Content("Approved!");
}
And This is its Razor View:
@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>
}
Now based on the razor view designs for both controller, the Requester can insert into two predefined fields which are title and amount and so is the finance manager which is Finance_Approval field only.
But the problem is that whenever the finance manager approves the request from his portal lets say with some value as "Yes", Then this value gets inserted into an entirely new row which does not have any connection with the rows filled by the requesters.
I want that finance manager should be able to approve the rows which has some value (which are filled by requesters), but i can not figure out the logic.
@erShoaib Here is the final updates:
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!");
}
This is the full index:
@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>
And this is the view for Details:
@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:
Your Requester Controller
and its View
was perfect means requester successfully added new record to db.
For example: title = "Abc"
, amount="123"
and after added this record suppose id
was 10
Now in Finance Controller
you need to get above record in Index
method like
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.
}
After change made by finance manager to Finance_Approval
field of above record with id
is 10
For example: Finance_Approval = "Approved"
. Then all the fields of record with this new field can be posted to Approve
action method in Finance Controller
.
And then you need to update above record like
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!");
}
来源:https://stackoverflow.com/questions/53084982/how-to-access-specific-rows-and-insert-values-into-specific-columns-of-a-table