In Details
action of PersonController
I would like to show every information about the Person
object.
Initially I want only to show some info about the Person
and rest will be collapsed, then I want to click a button/arrow/whatever and rest of info will expand.
Before clicking button/arrow/whatever all the data was loaded but just hidden. If I don't have to I don't want to use AJAX(I am not familiar with it).
Drawings show what I mean.
BEFORE CLICK
AFTER CLICK
Right now Details
view looks like that:
@model WebApplication2.Models.Person
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Person</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.FirstName)
</dt>
<dd>
@Html.DisplayFor(model => model.FirstName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.LastName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CellNumber)
</dt>
<dd>
@Html.DisplayFor(model => model.CellNumber)
</dd>
<dt>
@Html.DisplayNameFor(model => model.SecondaryPhoneNumber)
</dt>
<dd>
@Html.DisplayFor(model => model.SecondaryPhoneNumber)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Address)
</dt>
<dd>
@Html.DisplayFor(model => model.Address)
</dd>
<dt>
@Html.DisplayNameFor(model => model.BirthDate)
</dt>
<dd>
@Html.DisplayFor(model => model.BirthDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Pesel)
</dt>
<dd>
@Html.DisplayFor(model => model.Pesel)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Notes)
</dt>
<dd>
@Html.DisplayFor(model => model.Notes)
</dd>
<!-- SOMEWHERE HERE i WOULD LIKE TO INSERT THIS PARTIALL VIEW INTO EXPANDABLE COMPONENT -->
</dl>
</div>
EDIT: Pragnesh Khalas' original solution worked great but I wanted to pass whole Person
object to partial view. So I modified script replacing:
data: {
parentEls: '@Model.FirstName'
}
with
data: {
person: '@Model'
}
and in the controler:
[HttpPost]
public ActionResult Test(Person person) {
System.Diagnostics.Debug.WriteLine("PASSED: " + person);
return PartialView(person);
}
but Person person
in the Test
action is null
besides that everything works. Why it(model) hasn't been passed?
Full code:
Script:
<script>
function BtnOnclick() {
$.ajax({
type: 'POST',
url: '@Url.Content("~/Person/Test")',
data: {
person: '@Model'
},
success: function (data) {
$('#divpopup').css("display", "block");
$('#btnExpand').css("display", "none");
$('#divpopup')[0].innerHTML = data;
}
});
}
function CollapseDiv() {
$('#divpopup').css("display", "none");
$('#btnExpand').css("display", "block");
}
</script>
View:
<p>
<input type="button" value="Expand" id="btnExpand"
onclick="javascript:BtnOnclick();" />
</p>
<div id="divpopup" style="display:none">
</div>
Controller's action:
[HttpPost]
public ActionResult Test(Person person) {
// ViewBag.Chk = parentEls;
System.Diagnostics.Debug.WriteLine("PASSED: ");
System.Diagnostics.Debug.WriteLine(person.FirstName);
return PartialView(person);
}
}
Partial view:
@model WebApplication2.Models.Person
<hr />
<h2>Survey 1</h2>
<input type="button" id="Coll" value="Collapse" onclick="javascript:CollapseDiv()" />
<dt>
@Html.DisplayNameFor(model => model.Notes)
</dt>
<dd>
@Html.DisplayFor(model => model.Notes)
</dd>
<hr />