I have a strongly typed view with Model that represents multiple objects in List. On this view I generate Grid and each object represent one row in a grid. Then I pass specific object from this model List to partial view. This partial view is actually a popup window. On popup window I have a form where user can correct one row (one object) and submits back to Action, where I save changes to database. How can I pass specific object from view to partial view, when i click on specific "row"?
Is it possible to save for example ID of specific row to hidden field and then dynamically read this value from hidden field when rendering the partialview or when passing Model to partial view? Or is the whole concept wrong?
View Code
@model IEnumerable<Loyalty.Models.GridColumns>
@using GridMvc.Html
@using GridMvc.Sorting
<input type="button" id="opener" title="open popup" value="test1"/>
<a onclick="EditUser(1)">edit</a>
@Html.Grid(Model).AutoGenerateColumns()
<div id="EditUser" title="Popravi uporbnika">
@{
//Here I would like to get value from hidden field and change it with value 1
//in a WHERE sentance in the line below
Html.RenderPartial("EditUser", Model.Where(m => m.ID == 1).Single());
}
</div>
<script type="text/javascript">
$("#opener").click(function () {
$("#EditUser").dialog("open");
});
$(function() {
$("#EditUser").dialog({
open: function() {
$("body").addClass("blur");
},
clse: function () {
$("body").removeClass("blur");
},
modal: true,
closeText: "Close",
autoOpen: false,
width: 800,
height: 600
});
});
}
</script>
PartialView Code
@model Loyalty.Models.GridColumns
@using (Ajax.BeginForm("UpdateUser", "Home", new AjaxOptions () { UpdateTargetId="ID", OnSuccess="onSuccess()"}))
{
<fieldset class="PopUp">
<legend class="PopUp">General data</legend>
<div class="EditLabel">
Name:
</div>
<div class="EditValue">
@Html.TextBoxFor(m => m.FirstName)
</div>
<div class="EditLabel">
Last name:
</div>
<div class="EditValue">
@Html.TextBoxFor(m => m.LastName)
</div>
<div class="EditLabel">
Birth date:
</div>
<div class="EditValue">
@Html.TextBoxFor(m => m.BirthDate, "{0:dd-MMM-yyyy}")
</div>
<div class="EditLabel">
Gender:
</div>
<div class="EditValue">
@{
var item1 = new SelectListItem();
item1.Text = "male";
var item2 = new SelectListItem();
item2.Text = "female";
List<SelectListItem> items = new List<SelectListItem>();
items.Add(item1);
items.Add(item2);
var SelectItems = new SelectList(items, "Value", "Text");
}
@Html.DropDownListFor(m => m.Gender, SelectItems)
</div>
</fieldset>
<fieldset class="PopUp">
<legend class="PopUp">Address</legend>
<div class="EditLabel">
Street:
</div>
<div class="EditValue">
@Html.TextBoxFor(m => m.Street)
</div>
<div class="EditLabel">
House no.:
</div>
<div class="EditValue">
@Html.TextBoxFor(m => m.HouseNumber)
</div>
<div class="EditLabel">
Town:
</div>
<div class="EditValue">
@Html.TextBoxFor(m => m.Town)
</div>
<div class="EditLabel">
Post code:
</div>
<div class="EditValue">
@Html.TextBoxFor(m => m.PostNumber)
</div>
</fieldset>
<fieldset class="PopUp">
<legend class="PopUp">Contact info</legend>
<div class="EditLabel">
Phone:
</div>
<div class="EditValue">
@Html.TextBoxFor(m => m.PhoneNumber)
</div>
<div class="EditLabel">
E-mail:
</div>
<div class="EditValue">
@Html.TextBoxFor(m => m.Email)
</div>
</fieldset>
}
Main View Action
public ActionResult Index()
{
var UsersCollection = new List<GridColumns>();
for (int i = 1; i <= 31; i++)
{
bool disc = false;
if (i % 2 == 0)
{
disc = true;
}
else
{
disc = false;
}
UsersCollection.Add(new GridColumns() {ID = i, BirthDate = new DateTime(2014,1,i), Email = "user" + i + "@email.com", FirstName = "User1" + i, LastName = "User1LastName" + i, Gender = "male", Discount = disc, Cupons = i });
}
return View(UsersCollection);
}
PartialView Action
[HttpPost]
public ActionResult UpdateUser(GridColumns person)
{
return PartialView();
}
Partial View action is called when submitting popup form.
Model
[GridTable(PagingEnabled=true, PageSize=20)]
public class GridColumns
{
[GridColumn(Title="ID", SortEnabled=true)]
public int ID { get; set; }
[GridColumn(Title="First name", FilterEnabled=true, SortEnabled=true)]
public string FirstName { get; set; }
[GridColumn(Title = "Last name", FilterEnabled = true, SortEnabled = true)]
public string LastName { get; set; }
[GridColumn(Title = "Birth date", FilterEnabled = true, Width = "150", Format = "{0:dd-MMM-yyyy}")]
public DateTime BirthDate { get; set; }
[GridColumn(Title = "gender", FilterEnabled = true)]
public string Gender { get; set; }
[GridColumn(Title = "e-mail", FilterEnabled = true, SortEnabled = true)]
public string Email { get; set; }
[GridColumn(Title = "Phone", FilterEnabled = true, SortEnabled = true)]
public string PhoneNumber { get; set; }
[GridColumn(Title = "Street", FilterEnabled = true)]
public string Street { get; set; }
[GridColumn(Title = "House no.", FilterEnabled = true)]
public string HouseNumber { get; set; }
[GridColumn(Title = "Town", FilterEnabled = true)]
public string Town { get; set; }
[GridColumn(Title = "Post number", FilterEnabled = true)]
public int PostNumber { get; set; }
}
Html.RenderPartial("EditUser", Model.Where(m => m.ID == 1).Single());
and replace it with value from hidden field. – Gašper Sladič