When returning a view to an iframe in MVC4, the viewbag, viewdata, or tempdata doesnt pass information back to the overall view. - asp.net-mvc-4

I have a view that has some dropdowns that allows you to choose some surveys. When selected it loads the survey into an iframe. Loading the view into it works fine, but I am trying to display stats (summary) about this survey alongside the iframe. But since I'm returning the view to the iframe, the viewbag, viewdata, or tempdata doesnt return anything (imagine it does to the iframe), but I need it to return information to the overall view. Not sure how to do this.. Any help is greatly appreciated. Thanks
My view:
#using (Html.BeginForm("Survey", "Moderator", FormMethod.Post, new { target = modFrame" }))
{
<div id="jWizard" class="jWizard wizardStyle">
<table class="moderatorPage">
<tr>
<td > </td>
<td> </td>
<td> </td>
<td> </td>
<td>Study: #Html.DropDownList("SurveyID", Model.Surveys, new { onchange = "LoadGroups();", #class = "dropDownMain" })</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>Group: #Html.DropDownList("GroupID", Model.UserGroups, new { onchange = "LoadUsers();", #class = "dropDownMain" })</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>User: #Html.DropDownList("UserID", Model.Users, new { #class = "dropDownMain" })</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td><input type="submit" value="Start" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><iframe id="modFrame" name="modFrame" style="width:100%; height:1000px; border:0px"></iframe></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>#TempData["test"]</td>
<td>#ViewBag.test</td>
<td>#ViewData["test"]</td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
}
And in my controller:
public ActionResult Survey(int SurveyID, string GroupID, string UserID)
{
ViewBag.UserID = UserID;
if (SurveyID != 0)
{
TempData["test"] = "test1";
ViewData["test"] = "test2";
ViewBag.test = "test3";
var userTypeRepo = new UserTypeRepository(new UnitOfWork());
ViewBag.UserType = userTypeRepo.Get(user.UserTypeID).Description;
var repo = new SurveyRepository(new UnitOfWork());
var survey = repo.Get(SurveyID, "MasterPages,MasterPages.MasterTickets");
return View(survey);
}
else
{
return View("Error", (object)"Survey ID is zero");
}
}

Welcome to Stackoverflow!
The ViewBag in your iframe is not exposed to the ViewBagin your hosting page. In fact, you will need a page refresh in the hosting page to reload it's own ViewBag.
If I were you, I would re-architect the solution to make use of ajax services and a view engine based on JavaScript (e.g. AngularJS) to retrieve your dynamic data; however, if there is some reason that you have to stick with this iFrame approach, then you probably need to just reload your entire page on a selection change, populate the host page's viewbag with the relevant data, and set the source of the iframe with a ViewBag parameter as well.
Good luck!

Related

How to multiply two columns in a table together to produce a new column

I have 2 fields quantity and price. I basically want to multiply them together and get a value for another column called Price (Which is the total of the multiplication).
I have tried using the below html code:
#Html.DisplayFor(modelItem => item.item_order_quantity*item.ITEM.item_price)
This is my Table row code:
<table class="table table-striped table-advance table-hover">
<tbody>
<tr>
<th><i class="icon_pin_alt"></i> Item Description</th>
<th><i class="icon_pin_alt"></i> Quantity</th>
<th><i class="icon_calendar"></i> Price</th>
</tr>
#foreach (var item in Model.ITEM_ORDER)
{
<tr>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.ITEM.item_description)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity*item.ITEM.item_price)
</td>
<td>
<div class="btn-group">
#Html.ActionLink("View", "Details", new { id = item.OrderID }) |
#Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
</div>
</td>
</tr>
}
</tbody>
</table>
Consider having an additional property in your viewmodel or Model which does this task for you. Then you can bind this with html helper as you did for every other field.
public class YourViewModel
{
public int Field1{ get; set; }
public int Field2{ get; set; }
public int CalculatedField{
get {return Field1*Field2;}
}
}
Or try below code which calculates the value and stores in variable, then renders value directly from variable.
try this
<table class="table table-striped table-advance table-hover">
<tbody>
<tr>
<th><i class="icon_pin_alt"></i> Item Description</th>
<th><i class="icon_pin_alt"></i> Quantity</th>
<th><i class="icon_calendar"></i> Price</th>
</tr>
#foreach (var item in Model.ITEM_ORDER)
{
var computedValue = item.item_order_quantity*item.ITEM.item_price
<tr>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.ITEM.item_description)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity)
</td>
<td style="width:auto">
#(computedValue)
</td>
<td>
<div class="btn-group">
#Html.ActionLink("View", "Details", new { id = item.OrderID }) |
#Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
</div>
</td>
</tr>
}
</tbody>
</table>

kendo button click validation messages are not showing mvc4 razor

Here am using kendo button .and razor textboxes .Am not getting validation messages on button click
here is my code
<tr>
<td>
#Html.Label("User Name")
</td>
<td>
#Html.TextBoxFor(model => model.UserName, new { Class = "txtLogin" })
#Html.ValidationMessageFor(model => model.UserName)
</td>
</tr>
<tr>
<td>
#Html.Label("Password")
</td>
<td>
#Html.TextBoxFor(model => model.Password, new { Class = "txtLogin" })
#Html.ValidationMessageFor(model => model.Password)
</td>
</tr>
<tr>
<td colspan="2">
<div class="fielddiv">
#(Html.Kendo().Button()
.Name("btnsave")
.HtmlAttributes(new { type = "button" })
.Content("Login"))
</div>
</td>
</tr>
You need to check if everything is nested in a form tag (or Html.BeginForm). Then set the type of the button to "submit".

How to change MVC Web Grid structure?

I am trying to show list of items in a MVC 4 Web Grid control. But I am unable to re-format my web grid as table structure below.
Web Grid structure like this.
Table structure like this.
Here is my razor code for web grid.
#{
var grid = new WebGrid(Model.EnrollPlanPriceLevelList, canPage: true, rowsPerPage: 10, selectionFieldName: "id", ajaxUpdateContainerId: "gridContent");
grid.Pager(WebGridPagerModes.NextPrevious);
if (grid.HasSelection)
{
var EnrollPlanPriceLevelID = grid.Rows[grid.SelectedIndex].Value;
}
}
#grid.GetHtml(
tableStyle: "table table-condensed table-striped table-hover no-margin",
headerStyle: "",
mode: WebGridPagerModes.All,
columns:
grid.Columns(
grid.Column("", format: (item) => item.GetSelectLink(item.EnrollPlanPriceLevelID.ToString())),
grid.Column("SchoolYears.Name", "School Year"),
grid.Column("EnrollPlans.PlanName", "Enroll Plan"),
grid.Column("PriceLevels.Name", "Price Level"),
grid.Column("Price", "Price"),
grid.Column("", format: #<text>#Html.Label("Edit", new { #id = item.EnrollPlanPriceLevelID, #class = "badge", #onclick = "bindplanprogram(" + item.EnrollPlanPriceLevelID + ")" }) </text>),
grid.Column("", format: #<text>#Html.ActionLink("Delete", "Delete", new { id = item.EnrollPlanPriceLevelID }, new { #class = "badge" }) </text>)
))
Here is the table structure
<table class="table table-condensed table-striped table-hover no-margin">
<thead>
<tr>
<th>School Year
</th>
<th>Enroll Plan</th>
<th>
<span class="span3">Early Bird</span>
<span class="span3">Value Pricing</span>
<span class="span4">Standard Pricing</span>
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>2011-2012</td>
<td>A(k-8) Grade</td>
<td>
<span class="span3">$ 50</span>
<span class="span3">$ 60</span>
<span class="span3">$ 70</span>
</td>
<td>
<a class="badge">Edit</a>
<a class="badge">Delete</a>
</td>
</tr>
<tr>
<td>2011-2012</td>
<td>B (9-12)Grade</td>
<td>
<span class="span3">$ 70</span>
<span class="span3">$ 80</span>
<span class="span3">$ 90</span>
</td>
<td>
<a class="badge">Edit</a>
<a class="badge">Delete</a>
</td>
</tr>
<tr>
<td>2011-2012</td>
<td>Adult Ed. (age 18+)</td>
<td>
<span class="span3">$ 1500</span>
<span class="span3">$ 175</span>
<span class="span3">$ 200</span>
</td>
<td>
<a class="badge">Edit</a>
<a class="badge">Delete</a>
</td>
</tr>
</tbody>
</table>
I want to reformate my web grid structure as above table structure. Please tell me someone! How to change web grid structure accordingly?
The only way to achieve what you want with the WebGrid is to change the shape of the data you are passing in. Whether you do that in your SQL or whether you do that using LINQ to create a collection of anonymous types that have the appropriate properties is up to you, but the end result must be a collection of items that have public properties that match what you want as column names.

Accordion within table rows

I am using Twitter Bootstrap. Currently I am working on Master Details looks alike table, which I am planning to place an accordion into each table's row and if it is expanded, the partial view will be reload and displayed.
Somehow it does not work as expected but if I change it to Modal, the partial view loaded perfectly.
This is the controller that process and returning the values:
public ActionResult Index(int id,int? page)
{
List<CustomerProduct> customerProducts =
(from c in RepositoryProduct.Reload(id)
select c).ToList<CustomerProduct>();
return PartialView("_CustomerProducts", customerProducts);
}
and this is the table in the view :
<table id="tbl" class="table table-condensed" style="border-collapse:collapse;">
<thead>
<tr>
<th>
#Html.ActionLink("Customer Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
#Html.DisplayNameFor(model => model.First().CustLocalName)
</th>
<th>
#Html.ActionLink("Country", "Index", new { sortOrder = ViewBag.CountrySortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
#Html.DisplayNameFor(model => model.First().City)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr data-toggle="collapse">
<td style="display:none;">
#Html.DisplayFor(modelItem => item.CustID)
</td>
<td>
#Html.DisplayFor(modelItem => item.CustName)
</td>
<td>
#Html.DisplayFor(modelItem => item.CustLocalName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Country.CountryName)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
<a class="btn btn-small btn-info" type="button" href="#Url.Action("Index", "CustomerProduct", new { id = item.CustID })" data-toggle="collapse" data-target="##item.CustID" >Products</a>
</td>
</tr>
<tr>
<td colspan="6" class="hiddenRow">
<div id="#item.CustID" class="accordian-body collapse" tabindex="-1">
<div class="accordion-header">
<label>
<strong>#item.CustName product(s)</strong>
</label>
</div>
<div class="accordion-body">TEST</div>
</div>
</td>
</tr>
}
</tbody>
</table>
this is part of the code that calling the controller Index function :
<a class="btn btn-small btn-info" type="button" href="#Url.Action("Index", "CustomerProduct", new { id = item.CustID })" data-toggle="collapse" data-target="##item.CustID" >Products</a>
Actually I would like to load the partialview(_CustomerProduct) in the div class="accordion-body" as what I did in div class="modal-body" but no success. May I know if it is possible to do it and if yes, may I know how?
Any help will be appreciated.
for me to get the view to work i used
<td colspan="6" class="hiddenRow">
<div id="#item.CustID" class="accordian-body collapse" tabindex="-1">
<div class="accordion-header">
<label>
<strong>#item.CustName product(s)</strong>
</label>
</div>
<div class="accordion-body">
#Html.Partial("_CustomerProduct")
</div>
</div>
</td>

Saving the value of a Selectlist item when used in a for each loop in a view

Part of my MVC 3 VB.NET has a view that uses a list(of model) and a for each on that model to list all the contents.. The problem is that a selectlist is part of each item.. When the value is selected for each item it is not being posted back to the controller with the rest of the items... The view code is as follows:
#ModelTYPE List(Of xxxxxxx.attendance)
#Code
ViewData("Title") = "Class Attendance Record"
End Code
#Using Html.BeginForm
#<fieldset>
<table>
<tr>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Registrant ID
</th>
<th>
Course Status
</th>
<th>
Comments
</th>
</tr>
#For r As Integer = 0 To Model.Count - 1
Dim i As Integer = r
#Html.HiddenFor(Function(m) m(i).id)
#<tr>
<td>
#Html.DisplayFor(Function(m) m(i).firstName)
#Html.HiddenFor(Function(m) m(i).firstName)
</td>.
<td>
#Html.DisplayFor(Function(m) m(i).lastName)
#Html.HiddenFor(Function(m) m(i).lastName)
</td>
<td>
#Html.DisplayFor(Function(m) m(i).reg_id)
#Html.HiddenFor(Function(m) m(i).reg_id)
</td>
<td>
#Html.DisplayFor(Function(m) m(i).Completed_Class)
</td>
<td>
#Html.DropDownList("Completed_Class", New SelectList(ViewBag.courseStatus, "Status", "Status"), New With {.Completed_Class = "Completed_Class"})
#Html.HiddenFor(Function(m) m(i).Completed_Class)
</td>
<td>
#Html.TextBoxFor(Function(m) m(i).Comments, New With {.class = "AttenComment"})
#Html.HiddenFor(Function(m) m(i).Comments)
</td>
</tr>
Next
</table>
<p>
<input type="submit" name="submit" />
</p>
</fieldset>
End Using
Any ideas how I can assign the selectedvalue to the HiddenFor??
Instead of writing loops, I would recommend you using editor templates. Like this:
#ModelTYPE List(Of xxxxxxx.attendance)
#Code
ViewData("Title") = "Class Attendance Record"
End Code
#Using Html.BeginForm
#<fieldset>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Registrant ID</th>
<th>Course Status</th>
<th>Comments</th>
</tr>
#Html.EditorForModel()
</table>
<p><input type="submit" name="submit" /></p>
</fieldset>
End Using
and inside the corresponding editor template (~/Views/Shared/EditorTemplates/attendance.vbhtml or ~/Views/SomeController/EditorTemplates/attendance.vbhtml) which will be rendered for each element of the collection:
#ModelTYPE xxxxxxx.attendance
#Html.HiddenFor(Function(m) m.id)
#<tr>
<td>
#Html.DisplayFor(Function(m) m.firstName)
#Html.HiddenFor(Function(m) m.firstName)
</td>
<td>
#Html.DisplayFor(Function(m) m.lastName)
#Html.HiddenFor(Function(m) m.lastName)
</td>
<td>
#Html.DisplayFor(Function(m) m.reg_id)
#Html.HiddenFor(Function(m) m.reg_id)
</td>
<td>
#Html.DisplayFor(Function(m) m.Completed_Class)
</td>
<td>
#Html.DropDownList("Completed_Class", New SelectList(ViewBag.courseStatus, "Status", "Status"), New With {.Completed_Class = "Completed_Class"})
#Html.HiddenFor(Function(m) m.Completed_Class)
</td>
<td>
#Html.TextBoxFor(Function(m) m.Comments, New With {.class = "AttenComment"})
#Html.HiddenFor(Function(m) m.Comments)
</td>
</tr>
This being said, using all those hidden fields reminds me badly for the classic WebForms ViewState. You don't need it. Believe me, you really don't need this stuff. Use input fields if you allow the user to modify something (text fields, radios, checkboxes, etc...), but hidden fields, come on! A single hidden field containing the id of the model will largely suffice for the controller action to fetch the corresponding model from the datastore that it initially fetched it from.
As far as your dropdown is concerned:
#Html.DropDownList("Completed_Class", New SelectList(ViewBag.courseStatus, "Status", "Status"), New With {.Completed_Class = "Completed_Class"})
You should use a property on your view model to bind to and use the strongly typed version:
#Html.DropDownListFor(Function(m) m.CompletedCLass, New SelectList(ViewBag.courseStatus, "Status", "Status"), New With {.Completed_Class = "Completed_Class"})