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>