Master-Detail View ASP.NET MVC - asp.net-mvc

I'm new to MVC and I'm involved in a project that is developed with ASP.NET MVC 1.0. I'm also weak in JavaScript :-(
I'm trying to demonstrate how Master-Details view work on 'Orders' & 'Order Details' tables from Northwind database. (Hence: the tables has a relation, i.e., an order can have multiple order details)
I created two controls (1st for Orders, 2nd for OrderDetails). I displayed all the orders from Orders table into a List view. Once I click on one of the orders it takes me to the Details view of that order.
What I want to do (& failed) is to create a sub view below the Details view of the order that is having all the order details for that order.
I also want to change the content of the sub view based on the selections from the master view. I read a lot about using AJAX & JSON to dynamically change that but I failed to do it too :'(
Anyone can help in that and provide me with the technique & code of how I can implement it?

You can do this fairly easily with MVC and jQuery.
First in your Orders\List.aspx view:
<script>
// once the page has loaded
$(function() {
// set up your click event to load data
$('.list-item').click(function() {
// ajax load the content returned by the detail action
$('#detail').load('<%= Url.Action("Detail") %>', { id: this.id } );
});
});
</script>
<style> .list-item { cursor: pointer; } </style>
<% // loop through the orders in your model and show them
// as each div has the class list-item it will be give the click event
foreach( var order in Model ) { %>
<div id="<%= order.Id %>" class="list-item"><%= order.Name %></div>
<% } %>
<%-- the panel that the ajaxed content will be loaded into --%>
<div id="detail"></div>
Then in your Orders\Detail.ascx partial view:
Id: <%= Model.Id %><br />
Name: <%= Model.Name %><br />
Description: <%= Model.Description %><br />
etc

Related

Rails 4 - how to trigger Object edit form without refreshing the page

Hi and thanks for looking into this.
I have a list of objects displayed on my page. Now when a user selects one of the objects, I'd like to show Edit form for this object without refreshing the page and allow user to update it. I already have a form partial with :remote tag. What I cannot figure out is how to trigger this Edit form and pass the correct object into it.
Thanks in advance!
You can include the form partial for each item in an hidden div, and show the div with javascript only when needed :
<% #items.each do |item| %>
<div>
<%= item.label %>
Edit
<div class="editFormWrapper">
<%= render '_form', locals: {item: item} %>
</div>
</div>
<% end %>
css:
.editFormWrapper {
display: none;
}
javascript (with jquery)
$(document).ready(function() {
$('.editLink').on('click', function() {
$(this).siblings('editFormWrapper').show(); // or .slideDown(); or any other effect
});
});
If the page contains a lot of items, or if the hidden form is big, it may be better to use Ajax to request and include the edit form "on demand", to keep the weight of the page to minimum.
When you render page with objects, you can render objects array in javascript variable
<script>
OBJ_ARR = {
*obj_id1* = {
name: 'test1',
desc: 'test test'
},
*obj_id2* = {
name: 'test2',
desc: 'test2 test'
},
......
}
</scpipt>
Next we use javascript scenario
When object clicked you get certain object_id, and then you get appropriate object from our array
OBJ_ARR[object_id]
So, we put object properties to form and display that form

render partial view in MVC

I am using MVC Structure. I have to create a report which can be filtered by drop downs. I am thing to use a partial view to display report.
HEre is the structure of the page I want to achieve.
On top of page, there will be some drop down lists.
Below these will be page for report.
when user changes the options from dropdownlist, the report will be filtered.
I have two questions
1. How to render partial page.
2. How to refresh partial page through ajax/jquery. I want to do this on client side.
I have checked online, I am rendering page as shown in code below
VIEW
<h3>Report</h3>
<div>
<table>
<tr>
<td>ServiceLine</td>
<td>#Html.DropDownList("ServiceLine", null, new {id="ServiceLine"}) </td>
</tr>
</table>
</div>
<div>
<h2>List</h2>
<div>
#Html.Partial("PartialView")
</div>
</div>
This is what I have got in controller
public ActionResult PortfolioReport(char serviceLine)
{
//Department List
var serviceLines = Enum.GetValues(typeof(SogetiDepartments)).Cast<SogetiDepartments>().Select(v => new SelectListItem
{
Text = v.ToString(),
Value = ((char)v).ToString(),
});
foreach (SelectListItem item in serviceLines)
{
if (Convert.ToChar(item.Value) == serviceLine)
item.Selected = true;
}
ViewBag.ServiceLine = serviceLines;
return View();
}
Any kind of help is appreciated.
You have to use jQuery to achieve this feature
First apply some identifier to your data container
<div id="reportContent">
#Html.Partial("PartialView")
</div>
And then write script on your dropdown change event using jQuery
<script type="text/javascript">
$(function(){
$("#ServiceLine").change(function{
// get data from server using ajax
var url = 'YourPartialPageURL?'+serviceLine+="="+$(this).val();
$('#reportContent').load(url);
});
});
</script>
Note: You should use return PartialView(); from your controller action
And don't use #Html.Partial and use #Html.Action instead.
#Html.Partial loads View directly without going to Controller Action.
It should be used if you have data to be passed with you or if you just want to load some static content on the page

How to make IDs of components (Div, a, h1, etc) of a webpage Dynamic (variable?) using MVC2?

I am trying to do the following:
The object I give to the Viewpage has a list, I do a foreach in the HTML and I create a number of components. Now, I want the IDs of those components to somehow be linked to the object from the List.
(The reason I am trying to do this is because I want to show a button, when they press that button the shown content will change and they should see something else, I will use javascript to achieve this)
Therefor I am trying to make the id of those components dynamic, by for example stating
id="button <%= item.id%>" however this does not seem to work. I have searched alot on google but I haven't found a solution yet which is why I turn to you guys.
I'll link my code as well, I deleted some of the parts that were unnecessary (but added the javascript):
<script type="text/javascript">
function AlterPanel(thePanel) {
var panel = document.getElementById("region"+thePanel);
panel.style.display = 'block';
var button = document.getElementById("button"+thePanel);
button.style.display = 'none';}
</script>
<%foreach (TeamDTO team in Model.List.Teams)
{ %>
<a id="button<%= team.Number %>" onclick="AlterPanel(<% team.Number%>)">
Add member</a>
<div Visible="false" id='region<%= team.Number %>' runat="server">
Please select one:
<%: Html.DropDownListFor(V => V.memberID, new SelectList(Model.members, "ID","Name")) %>
</div>
<% } %>
I eagerly await a reply and thank you in advance.
I guess your Queastion is:
-you have some "button DropDownList" pair, button is visiable, DropDownList is invisible, now if user click the button then DropDownList will showup.
OK, now your View maybe :
<%foreach (TeamDTO team in Model.List.Teams)
{ %>
<a onclick="AlterPanel(<% team.Number%>)">
Add member</a>
<div id="region<%= team.Number %>" style="display:none">
Please select one:
<%: Html.DropDownListFor(V => V.memberID, new SelectList(Model.members, "ID","Name")) %>
</div>
<% } %>
I use JQuery in javascript part like this:
<script type="text/javascript">
function AlterPanel(thePanel) {
$("#region" + thePanel.toString()).css("display", "block");
}
</script>
Don't forget include the following file in View():
<script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery-1.4.1.min.js") %>"></script>
if the answer is not what you want, let mt know and I can help you~ :)

choose which controller to use in a ASP.NET MVC form

I have a searchform in my mvc application where i choose in a dropdownlist what department i want to search in, and a textinput on what i want to search on.
when i choose a department in my dropdownlist i want to forms controller to change to the selected value.
Lets say i have these items i my dropdownlist: "Customers", "Products", "Invoices"
when i choose "Customers" in my dropdownlist i want my Html.BeginFrom to look like this:
<% using (Html.BeginForm("Customers", Request.QueryString["Search"], "Search", FormMethod.Get))
{ %>
and when i select "Products" i want "Customers" to change to "Products".
I agree this isn't the best way to approach things, however if you still need to do it this way then a simple jquery statement is what you'll need.
Basically handle the onSubmit action of the form (or the onChange of the dropdown list --both will work) and change the form's action based on the value of the drop down list
Something like this should work:
<% using (Html.BeginForm()) { %>
<%=Html.DropDownListFor( x => x.DepartmentList ) %>
<input type='submit' value='submit'/>
<% } %>
<script type="text/javascript">
$(function(){
$('#DepartmentList').change(function(){
$('form').attr('action', $('#DepartmentList option:selected').val() );
});
});
</script>
Check out the answer of this question for more details
This is generally not the way you do things.
If you need two different things to happen depending on the posted value of a dropdown you should handle the logic dispatching inside of the Controller.
public ActionResult DoSomething(string actionType )
{
if( actionType == "Products" )
DoSomething();
if( actionType == "Customers" )
DoSomethingDifferent();
}

RenderAction needs to behave different depending on containing page

I use RenderAction to render a partial that is used all over my site.
It is a partial where the user can search for an entity. It depends on the Controller / Action that rendered the parent main view what is done once the entity is found.
Lets say I have the controllers:
HireController, FireController with
Action ActOnPerson and
PeopleController with Action FindPerson which renders the partial
FindPerson
The Views are Hire/SearchPerson.aspx and Fire/SearchPerson.aspx
Each View contains the helper:
<%Html.RenderAction("FindPerson ", "People"); %>
The form that posts to HireController/FireController is contained in the partial.
It needs to be this way, because there are actually a couple of steps (form posts) involved in finding a person.
Is there a way to decide inside the partial FindPerson if the form needs to be posted to FireController or HireController? I guess I am looking for something like public properties of WebControls but for RenderAction.
Just add parameter ("PostTo" or "Next") to People.FindPerson Action:
<% Html.RenderAction("FindPerson ", "People", new { next = Url.Action("ActOnPerson", "HireController") }); %>
<!-- or -->
<% Html.RenderAction("FindPerson ", "People", new { nextaction = "ActOnPerson", nextcontroller = "HireController" }); %>
In FindPerson PartialView:
<form method="post" action="<%= ViewData["next"].ToString() %>">
<!-- or -->
<% using (Html.BeginForm(
ViewData["nextaction"].ToString(), ViewData["nextcontroller"].ToString() ) { %>

Resources