Reputation: 88
I'm currently trying to find out what´s the MVC-way to solve the following problem:
I have a table (index view) based on a model and my first row has a button to change one cell of this row. When the button is pressed a popup is shown (via Ajax.ActionLink
), which asks for an additional comment. The popup view is realised with a Partial View and a Html.BeginForm
. After adding a comment and clicking the submit button the updated index view is shown.
At the moment I calculate the id of the first-row item before the updated index view is shown, because I don't know how to pass this id via the Ajax.ActionLink
at first and the Html.BeginForm
afterwards.
The whole process looks like this:
Ajax.ActionLink in Index View:
@Ajax.ActionLink("Update", "Update", null,
new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "result", InsertionMode = InsertionMode.Replace, OnSuccess = "openPopup()" },
new { @class = "btn btn-info"})
Update Partial View:
@using (Html.BeginForm("Index"))
{
<div class="form-group">
<h4>Title</h4>
<p>Comment here:</p>
<textarea type="text" class="form-control" name="comment" id="comment" required></textarea>
</div>
<input type="submit" value="Update" class="btn btn-info" />
}
ActionResults in Controller
[HttpGet]
public PartialViewResult Update()
{
return PartialView();
}
[HttpPost]
public async Task<ActionResult> Index(string comment)
{
//Here I calculate the ID, which represents the first row
//I would like to have this ID as a paramter, additional to comment
int maxID = dbContext.db.Max(m => m.pmc_id);
db x= dbContext.db.Where(m => m.pmc_id == maxID).FirstOrDefault();
//...Update database etc.
return RedirectToAction("Index");
}
Any help is appreciated!
Upvotes: 1
Views: 177
Reputation: 151
first pass the Id to view with for example viewbag or some view model in here I chose ViewBag
[HttpPost]
public async Task<ActionResult> Index(string comment)
{
//Here I calculate the ID, which represents the first row
//I would like to have this ID as a paramter, additional to comment
int maxID = dbContext.db.Max(m => m.pmc_id);
db x= dbContext.db.Where(m => m.pmc_id == maxID).FirstOrDefault();
//...Update database etc.
ViewBag.CurrentId = calculatedId;//saved Id to pass to View
return RedirectToAction("Index");
}
lets add into the view the Id
@using (Html.BeginForm("Index"))
{
<input type="hidden" value="@ViewBag.CurrentId" name="Id"/>
<div class="form-group">
<h4>Title</h4>
<p>Comment here:</p>
<textarea type="text" class="form-control" name="comment" id="comment" required></textarea>
</div>
<input type="submit" value="Update" class="btn btn-info" />
}
now lets get the Id parameter with mvc parser
[HttpGet]
public PartialViewResult Update(int Id)//here is your id
{
return PartialView();
}
Upvotes: 1