Cannot POST complex object from View to Controller - asp.net-mvc

So I've read many of the questions already about this and can't seem to find a correlation. I have a view model that consists of another model with several collections of - you guessed it - more models:
Public Class FeeTemplateViewModel
Public Property FeeSetup As FeeSetupModel
Public Property TemplateId As Integer
Public Property Name As String
Public Property CompanyId As Integer
Public Property GroupId As Integer
Public Property Users As Integer
Public Property CreateDate As DateTime
Public Property Modified As DateTime
Public Property CreatedBy As String
End Class
Public Class FeeSetupModel
<DisplayName("Office Id")>
Property OfficeId() As String
<DisplayName("Office Name")>
Property OfficeName() As String
<DisplayName("Current Fees")>
Property CurrentFees() As IEnumerable(Of FeeItemModel)
<DisplayName("Minimum Preparer Fee")>
Public Property MinPFee As Decimal
Public Property AvailableFees As List(Of FeeListItem)
Public Property CustomFees As List(Of CustomFee)
End Class
When I post back to the controller from the view, FeeSetup is null and I can't determine why.
Controller:
<HttpPost()>
Function Edit(model As FeeTemplateViewModel) As ActionResult
ModelState.Clear()
If String.IsNullOrEmpty(model.Name) Then
ModelState.AddModelError("Name", "Template name is required")
End If
If Not ModelState.IsValid Then
Return View("Edit", model)
End If
Save(model)
Return View("Edit")
End Function
ModelState error:
{"The parameter conversion from type 'System.String' to type 'ProAvalon.Models.Configuration.FeeSetupModel' failed because no type converter can convert between these types."}
View:
#Using Html.BeginForm("Edit", "FeeTemplate", Nothing, FormMethod.Post, Nothing)
#<div>
#Html.HiddenFor(Function(m) m.FeeSetup)
#If Model.TemplateId > 0 Then
#<p><strong>Last Modified:</strong>#Model.Modified.ToLocalTime</p>
End If
<div class="inputsection col-md-14" id="options">
<div class="row">
<div>
#Html.LabelFor(Function(m) m.Name, "Template Name")
#Html.EditorFor(Function(m) m.Name, "TemplateName")
</div>
</div>
#Html.ValidationMessageFor(Function(m) m.Name)
<div style="height:50px;line-height:50px;">
<div id="status-bar" style="display:none">
<span style="font-size:16px; margin-left:5px; font-weight:bold; color: #333;" />
</div>
</div>
<div class="clearfix" />
<div id="fee-grid-wrapper" style="min-height:500px;">
<table id="fee-grid" class="table table-striped table-condensed table-bordered table-fixed-header">
<thead>
<tr class="alert-info">
<th style="border:none;vertical-align:top;text-transform:uppercase;">
<strong>Filter Forms</strong>
</th>
<th style="border: none; vertical-align: top;" colspan="3">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12" style="padding:0 5px">
<input name="filter" type="radio" checked="checked" data-filter="" /> All forms
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12" style="padding:0 5px">
<input name="filter" type="radio" data-filter="fd" /> Federal Forms <br />
<input name="filter" type="radio" id="state-forms" /> State Forms
<div style="display:none" id="state-form-picker" class="state-picker">
#Html.DropDownListFor(Function(m) i, ProAvalon.PageHelpers.StateSelectList)
</div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12" style="padding:0 5px" id="search-target">
</div>
</th>
</tr>
<tr style="background:#eee">
<th style="cursor:pointer">No.</th>
<th style="cursor:pointer">Fed/St</th>
<th style="cursor:pointer">Form/Schedule Name</th>
<th style="cursor:pointer">Amount</th>
</tr>
</thead>
<tfoot>
<tr style="background:#eee">
<th>No.</th>
<th>Fed/St</th>
<th>Form/Schedule Name</th>
<th style="min-width:300px">Amount</th>
</tr>
</tfoot>
<tbody>
#If Model.FeeSetup.AvailableFees IsNot Nothing Then
For Each fee In Model.FeeSetup.AvailableFees
#Html.HiddenFor(Function(m) fee)
#Code
Dim tmp As ProAvalon.Models.Configuration.FeeItemModel = Model.FeeSetup.CurrentFees.Where(Function(x) x.FormName = fee.FormName).FirstOrDefault()
Dim amount = 0D
If (tmp IsNot Nothing) Then amount = tmp.FeeAmount
i = i + 1
Html.HiddenFor(Function(m) tmp)
End Code
#<tr data-id="#(fee.FormName)">
<td>#(i) </td>
<td>#(IIf(fee.FormType = "FD", fee.FormType, IIf(fee.FormType = "BS", fee.FormState + "-BS", fee.FormState)))</td>
<td>#(fee.FormDesc)</td>
<td style="min-width:300px">
<span style="display:none">#amount</span> #*to enable sorting*#
#Html.TextBoxFor(Function(m) amount)
#*#(New HtmlString("<span>" + _
Html.TextBox("Amount_" + fee.FormName.ToString(), CType(amount, Decimal), _
New With {.class = "pull-left currency_input currencyTB amount_" + fee.FormName}).ToHtmlString() + _
"<span id=""msg_" + fee.FormName + """ style='display:none; line-height:35px;padding-left:10px;'></span></span>"))*#
</td>
</tr>
Next
End If
</tbody>
</table>
</div>
</div>
</div>
#<br />
#<div>
<hr />
<div class="float-right" style="margin-left: 20pt">
#Html.SubmitButton()
</div>
<div>
#Html.CancelButton("Index", "FeeTemplate")
</div>
<div class="clear clearfix" />
</div>
End Using
I'm going to feel like a real jerk if one of you looks at this and finds a naming overlap, but I've looked through it several times and cannot determine that to be the cause.

To begin, list binding does not work on this:
For Each fee In Model.FeeSetup.AvailableFees
You'll need to look through it using an index:
For index As Integer = 0 To Model.FeeSetup.AvailableFees.Length
#Html.HiddenFor(Function(m) m.FeeSetup.AvailableFees[index])
Next

Related

.Net 6 - Passing a radio button selected table row to a controller

I really feel like this should be easy but I’m thinking it may have changed with .Net 6. I can pass values to my controller with the input “name=’name’” but for some reason I cannot get any values from my model into my controller. I am trying to POST my row values to the controller. I am using an enumerable. I’m not sure if I should be using a or not. Another thing is how should I be populating my table row from a loop of the model. I thought using #Html. Was for older .net and tag helpers are the new way but I couldn’t get any to work populating my rows.
<form method="post">
<div id="tblPullParts" class="container justify-content-center mt-3">
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th >Order #</th>
<th >Item</th>
<th >Description</th>
<th >Quantity</th>
</tr>
</thead>
<tbody>
#foreach (var p in Model)
{
<tr>
<td><input type="radio" id="radio" name="radio"
value="#Html.DisplayFor(item => p.PartID)" /></td>
#*<td><input asp-for="Selected" type="radio" value="Selected" /></td>*#
<th scope="row">#Html.DisplayFor(item => p.PartID)</th>
<td>#Html.DisplayFor(item => p.Name)</td>
<td>#Html.DisplayFor(item => p.ItemLocation)</td>
<td>#Html.DisplayFor(item => p.PartGroup)</td>
<td>#Html.DisplayFor(item => p.Description)</td>
<td>
<input type="text" asp-for="#p.Name" id="txtNameN" />
</td>
</tr>
}
</tbody>
</table>
#*<input type="text" id="#Model[0].Name" />*#
<input type="text" id="txtName" name="txtName" value="" />
</div>
<div class="text-center">
<button type="submit" class="btn btn-lg btn-success mt-3">Start Pick</button>
</div>
</form>
[HttpPost]
public async Task<IActionResult> Index( PartVM model, string radio, string txtName)
{
if (model?.PartID != 0)
{
return View("UpdatePickQuantity", model);
}
if (!String.IsNullOrWhiteSpace(txtName))
{
}
//Request.QueryString["radio"];
var lstParts = await _ordersService.GetAllParts();
return View(lstParts);
}
#Html.DisplayFor() can only display the value of model, If you want to submit the value, You need to use <input>,Here is a simple demo, I add hidden input in your form to submit the value:
#model List<PartVM>
#{
int i = 0;
}
<form method="post">
<div id="tblPullParts" class="container justify-content-center mt-3">
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th >Order #</th>
<th >Item</th>
<th >Description</th>
<th >Quantity</th>
</tr>
</thead>
<tbody>
#foreach (var p in Model)
{
<tr>
<td><input type="radio" id="radio" name="radio"
value="#Html.DisplayFor(item => p.PartID)" /></td>
#*<td><input asp-for="Selected" type="radio" value="Selected" /></td>*#
<th scope="row">
#Html.DisplayFor(item => p.PartID)
<input type="hidden" asp-for="#p.PartID" name="[#i].PartID">
</th>
<td>
#Html.DisplayFor(item => p.Name)
</td>
<td>
#Html.DisplayFor(item => p.ItemLocation)
<input type="hidden" asp-for="#p.ItemLocation" name="[#i].ItemLocation">
</td>
<td>
#Html.DisplayFor(item => p.PartGroup)
<input type="hidden" asp-for="#p.PartGroup" name="[#i].PartGroup">
</td>
<td>
#Html.DisplayFor(item => p.Description)
<input type="hidden" asp-for="#p.Description" name="[#i].Description">
</td>
<td>
<input type="text" asp-for="#p.Name" name="[#i].Name" id="txtNameN" />
</td>
</tr>
i++;
}
</tbody>
</table>
#*<input type="text" id="#Model[0].Name" />*#
<input type="text" id="txtName" name="txtName" value="" />
</div>
<div class="text-center">
<button type="submit" class="btn btn-lg btn-success mt-3">Start Pick</button>
</div>
</form>
Controller
[HttpPost]
public async Task<IActionResult> Index( List<PartVM> model, string radio, string txtName)
{
//.....
}
Demo:
Your second question can refer to this github issue.
Edit============================
If you want just want to pass the row where radio is selected, you need to js to achieve this. refer to this:
Create a ViewModel
public class PartvmViewModel
{
public int PartID { get; set; }
public string Name { get; set; }
public string ItemLocation { get; set; }
public string PartGroup { get; set; }
public string Description { get; set; }
public string? txtName { get; set; }
}
controller
public IActionResult Display()
{
List<PartvmViewModel> viewmodel = new List<PartvmViewModel>();
//pass data from PartVM to PartvmViewModel
foreach (var item in PartVms)
{
viewmodel.Add(new PartvmViewModel()
{
PartID = item.PartID,
Description = item.Description,
ItemLocation = item.ItemLocation,
Name = item.Name,
PartGroup = item.PartGroup
});
}
return View(viewmodel);
}
[HttpPost]
public IActionResult Display([FromBody]PartvmViewModel model)
{
//because the value of radio is equal to PartID,SO i don't write it as parameter here
//.........
}
View
#model List<PartvmViewModel>
#{
int i = 0;
}
<form method="post">
<div id="tblPullParts" class="container justify-content-center mt-3">
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th >Order #</th>
<th >Item</th>
<th >Description</th>
<th >Quantity</th>
</tr>
</thead>
<tbody>
#foreach (var p in Model)
{
<tr>
<td><input type="radio" name="radio" onclick="Method(this)"
value="#Html.DisplayFor(item => p.PartID)" /></td>
#*<td><input asp-for="Selected" type="radio" value="Selected" /></td>*#
<th scope="row">
#Html.DisplayFor(item => p.PartID)
<input type="hidden" asp-for="#p.PartID" name="[#i].PartID" id="PartID">
</th>
<td>
#Html.DisplayFor(item => p.Name)
</td>
<td>
#Html.DisplayFor(item => p.ItemLocation)
<input type="hidden" asp-for="#p.ItemLocation" name="[#i].ItemLocation" id="ItemLocation">
</td>
<td>
#Html.DisplayFor(item => p.PartGroup)
<input type="hidden" asp-for="#p.PartGroup" name="[#i].PartGroup" id="PartGroup">
</td>
<td>
#Html.DisplayFor(item => p.Description)
<input type="hidden" asp-for="#p.Description" name="[#i].Description" id="Description">
</td>
<td>
<input type="text" asp-for="#p.Name" name="[#i].Name" id="txtNameN" />
</td>
</tr>
i++;
}
</tbody>
</table>
#*<input type="text" id="#Model[0].Name" />*#
<input type="text" id="txtName" name="txtName" value="" />
</div>
<div class="text-center">
<button class="btn btn-lg btn-success mt-3" onclick="Submit()">Start Pick</button>
</div>
</form>
#section Scripts
{
<script>
var Data;
function Method(obj){
this.Data = {
"PartID":$(obj).val(),
"ItemLocation" : $(obj).parent().parent().find('td:eq(2)').find(':input').val(),
"PartGroup" : $(obj).parent().parent().find('td:eq(3)').find(':input').val(),
"Description" : $(obj).parent().parent().find('td:eq(4)').find(':input').val(),
"Name" : $(obj).parent().parent().find('td:eq(4)').find(':input').val(),
}
}
function Submit(){
Data.txtName = $("#txtName").val();
$.ajax({
url : '/Home/Display',
type : 'post',
data : JSON.stringify(Data),
contentType : 'application/json'
});
}
</script>
}
My opinion==========================
Actually, In my opinion, Pass the list of data is ok. Because i notice that you pass the value of radio into the controller, You can just use:
[HttpPost]
public async Task<IActionResult> Index( List<PartVM> model, string radio, string txtName)
{
var item = model.Where(i => i.PartID == int.Parse(radio)).FirstOrDefault();
//.....
}
to get the selected row, It is more easier than using js;
Instead of doing this
<th scope="row">
#Html.DisplayFor(item => p.PartID)
<input type="hidden" asp-for="#p.PartID" name="[#i].PartID" id="PartID">
</th>
I would do something like
<th scope="row">
<p>#p.PartID</p>
<input type="hidden" asp-for="#p.PartID" name="[#i].PartID" id="PartID">
</th>
However, I could be wrong because you are using razor syntax and I am not use to that yet ;)

Why is my anchor tag in Thymeleaf is not redirecting to the local file

I am trying to redirect to another local web page using a tag in Thymeleaf and Spring boot but it is
not working. I am redirecting from index.html to addEdit.html which are in the same folder.
Here is my code.
index.html
<div class="container">
<p th:text="${message}"></p>
<a th:href="#{/addEdit.html}" class="btn btn-outline-info">Add Employee</a> //not working
<table class="table table-bordered table-dark">
<thead class="">
<tr>
<th>#</th>
<th>Name</th>
<th>Departmen</th>
<th>Position</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="employee : ${employees}" >
<th th:text="${employee.id}"></th>
<td th:text="${employee.name}"></td>
<td th:text="${employee.department}"></td>
<td th:text="${employee.position}"></td>
<td>
<form action="delete">
<input type="submit" value="Delete" class="btn btn-outline-warning"/>
</form>
</td>
<td>
<form action="edit">
<input type="submit" value="Edit" class="btn btn-outline-info"/>
</form>
</td>
</tr>
</tbody>
</table>
</div>
my EmployeeController
#Autowired
private employeeRepo repo;
#RequestMapping("/")
public String home(Model model) {
List<Employee> list = new ArrayList<>();
list = repo.findAll();
model.addAttribute("employees",list);
return "index";
}
#PostMapping("/addEmployee")
public void addEmployee(Employee employee,Model model) {
repo.save(employee);
model.addAttribute("message","Add Successfully");
home(model);
}
my addEdit.html
<div class="container bg-light">
<form action="addEmployee">
<input class="form-control form-control-sm" type="text" placeholder="Name" name="name"><br>
<input class="form-control form-control-sm" type="text" placeholder="Department" name="department"><br>
<input class="form-control form-control-sm" type="text" placeholder="Position" name="postion"><br/>
<input type="submit" value="Add Employee" class="btn btn-outline-success btn-lg btn-block">
</form>
</div>
You should not include the .html in your link. The link should point to a URL that your controller exposes. There is currently no controller method that exposes the /addEdit url for example.
So update your controller:
#GetMapping
public String addEmployee(Model model) {
// add model attributes here as needed first
return "addEdit" // This refers to the view, so 'addEdit.html'
}
Now update the link to:
<a th:href="#{/addEdit}" class="btn btn-outline-info">Add Employee</a>

Binding List with checkbox and post back [duplicate]

This question already has answers here:
Post an HTML Table to ADO.NET DataTable
(2 answers)
Closed 4 years ago.
I have a object that I am binding to my view. This object has a list of items with a boolean field on them. I use this boolean field to bind to a checkbox on the form. I am wanting the user to select the items they want and then submit. This issue is, when it is submitted and passed to the post action, the list of items is null.
Here is the View Model passed into the view.
public class ReAssignVM
{
public string ToUsername { get; set; }
public string FromUsername { get; set; }
public IEnumerable<ReAssignTaskVM> Tasks { get; set; }
} // End of Class
Here is the view:
#model SRM.ClassLibrary.V1.ViewModels.TaskViewModels.ReAssignVM
#{
ViewData["Title"] = "Re-Assign Tasks";
}
<h2>Re-Assign Tasks</h2>
<br />
<div class="row">
<div class="col-md-1 alignRight">
<b>From:</b>
</div>
<div class="col-md-3">
#Model.FromUsername
</div>
<div class="col-md-1 alignRight">
<b>To:</b>
</div>
<div class="col-md-3">
#Model.ToUsername
</div>
</div>
<br />
<form asp-action="ReAssignTasks">
<input type="hidden" asp-for="FromUsername" />
<input type="hidden" asp-for="ToUsername" />
<div include-if="#Model.Tasks != null && Model.Tasks.Count() > 0">
<table class="table table-hover">
<thead class="thead-light">
<tr>
<th></th>
<th>
Status
</th>
<th>
Date
</th>
<th>
Student
</th>
<th>
Description
</th>
</tr>
</thead>
<tbody>
#foreach (var task in Model.Tasks.OrderBy(n => n.TaskDate))
{
<tr>
<td>
#Html.HiddenFor(item => task.TaskID)
#Html.CheckBoxFor(item => task.Assign)
</td>
<td>
#Html.DisplayFor(item => task.TaskStatus)
</td>
<td>
#task.TaskDate.ToLocalTime().ToString("M/d/yyyy")
</td>
<td>
#Html.DisplayFor(item => task.StudentUsername)
</td>
<td>
#Html.DisplayFor(item => task.Description)
</td>
</tr>
}
</tbody>
</table>
<br />
<div class="row">
<div class="col-md-12 alignRight">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#assignment">
Re-Assign
</button>
</div>
</div>
</div>
<div class="modal fade" id="assignment" tabindex="-1" role="dialog" aria-labelledby="assignmentLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="assignmentLabel">Confirm Re-Assignment of Tasks?</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Selected tasks will be re-assigned from #Model.FromUsername to #Model.ToUsername</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<input type="submit" value="Confirm" class="btn btn-primary" />
</div>
</div>
</div>
</div>
</form>
<div exclude-if="#Model.Tasks != null && Model.Tasks.Count() > 0">
<div class="alert alert-info">
There are no tasks to re-assign.
</div>
</div>
Can someone help me understand how to do this correctly?
You will need to provide the indexing to your Collection items to posted back in the model as Model binder would use those to bind back the objects to action. So you should be using For loop and IList<T> as your model in view :
#for(int i=0; i< Model.Tasks.Count(); i++)
{
#Html.HiddenFor(item => Model.Tasks[i].TaskID)
#Html.CheckBoxFor(item => Model.Tasks[i].Assign)
............
............
}
and in your model change it to be IList<T> as you wouldn't have indexer on IEnumerable<T>:
public class ReAssignVM
{
public string ToUsername { get; set; }
public string FromUsername { get; set; }
public IList<ReAssignTaskVM> Tasks { get; set; }
} // End of Class

HttpPostedFileBase is coming null

below is the code in my View
#using ClaimBuildMVC.Models
#model IEnumerable<Asset>
#{
ViewBag.Title = "AssetsPage";
Layout = "~/Views/Shared/_SuperAdminLayout.cshtml";
}
<script type="text/javascript">
</script>
#using (Html.BeginForm("AssetsPage", "SuperAdmin", FormMethod.Post,new{enctype = "multipart/form-data"}))
{
<div class="Content-inner-pages">
<div class="TopHeading TopHeading2">
<h2>Assets</h2>
<a class="CreateBtn AssetsBtn" href="Javascript:void(0);" onclick="javascript:$('#hdnIsNew').val('1')">Add Asset</a>
<div class="clearfix"></div>
</div>
<input type="hidden" id="hdnIsNew" value="1" />
<input type="hidden" id="hdnRecId" />
<!-- Slide Popup panel -->
<div class="cd-panel from-right AddAssetForm">
<header class="cd-panel-header">
<h3>Add Asset</h3>
Close
</header>
<div class="cd-panel-container">
<div class="cd-panel-content">
<div class="form-horizontal form-details popup-box">
<div class="form-group">
<label class="col-md-5 control-label">
Asset Title
</label>
<div class="col-md-7">
<input type="text" id="txtTitle" name="txtTitle" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-md-5 control-label">Description</label>
<div class="col-md-7">
<textarea id="txtDesc" class="form-control" cols="5" rows="5"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-5 control-label">Attachment</label>
<div class="col-md-7">
<input type="file" id="file" class="custom-file-input">
</div>
</div>
<div class="form-group">
<div class="col-md-7 col-md-offset-5">
<input type="submit" value="Save" name="actionType" class="btn-class btn-success">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="box">
<div class="box-content Custom-DataTable">
<table id="AdministationAssets" class="table table-hover dt-responsive CustomDatable AdministationAssetsTable" cellspacing="0" width="100%">
<thead>
<tr>
<th style="width:5%;">Assets</th>
<th style="width:15%;">
#Html.DisplayNameFor(model =>model.Title)
</th>
<th style="width:50%;">
#Html.DisplayNameFor(model =>model.Description)
</th>
<th style="width:8%;">Options</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td></td>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem =>item.Description)
</td>
<td>
#Html.ActionLink("Edit", "AddEditRecord", new{ id = item.ID }, new { #class = "ActionEdit AssetEdit", onclick ="javascript:GetEditDetails(" + item.ID + ");" })
#Html.ActionLink("Delete", "AssetDelete", new{ id = item.ID }, new { #class = "ActionDelete", onclick = "return confirm('Are You Sure delete this record?');", })
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
and below is my controller that what i want to call when click on save button but 'img' is coming as null and after searching in google i found that to add #using(Html.BeginForm()) but still it is coming as null
[HttpPost]
public ActionResult AssetsPage(Asset ast, HttpPostedFileBase file)
{
using (GenericUnitOfWork gc = new GenericUnitOfWork())
{
if (HttpContext.Request.Files.Count > 0)
{
ast.ContainerName = "reports";
ast.BlobName = HttpContext.Request.Files[0].FileName;
ast.BlobUrl = BlobUtilities.CreateBlob(ast.ContainerName, ast.BlobName, null, GetStream(HttpContext.Request.Files[0].FileName));
ast.FileName = HttpContext.Request.Files[0].FileName;
}
gc.GetRepoInstance<Asset>().Add(ast);
gc.SaveChanges();
}
return RedirectToAction("AssetsPage");
}
Not able to find the solution. Please help or give some reference if possible.
Asp.Net MVC default model binding works with name attribute, So add name="file" attribute with input type="file" as shown :-
<input type="file" name="file" id="file" class="custom-file-input">

Return multiple views to one ActionResult with ASP.NET MVC

What I want to achieve essentially is:
Items assigned to me
Item 1 assigned to me
Item 2 assigned to me
Item 3 assigned to me
All open items
Item 1 open to everyone
Item 2 open to everyone
Item 3 open to everyone
Item 4 open to everyone
Though from what I have experienced of MVC so far is that I would have to return the data to the view model to be able to use it in the view itself in the following manner:
<asp:Content ID="ticketsContent" ContentPlaceHolderID="MainContent" runat="server">
<div id="hdMain">
<div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
<div id="hdMainContent">
<div id="numberOfCalls">Calls assigned to you (<%=Html.ViewData("MyOpenCallsCount")%>)</div>
<div id="assignedToMe">
<div id="callHeaders">
<table id="callHeadersTbl" cellpadding="0" cellspacing="0">
<tr>
<td width="54"> </td>
<td width="270">Subject</td>
<td width="148">Logged</td>
<td width="120">Updated</td>
</tr>
</table>
</div>
<div id="greyTicketBar"> Assignee: <strong><%=Html.ViewData("UserName")%></strong></div>
<table cellpadding="0" cellspacing="0" width="643">
<% For Each aT In ViewData.Model%>
<tr>
<td width="54" class="ticketList"> </td>
<td width="270" class="ticketList"><%=Html.ActionLink(aT.Title, "Details", New With {.id = aT.CallID})%></td>
<td width="148" class="ticketList"><%=aT.loggedOn.Date.ToShortDateString%></td>
<td width="115" class="ticketList"><%=aT.updatedOn.Date.ToShortDateString%></td>
</tr>
<% Next%>
</table>
</div>
</div>
<div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
<div id="bigbreak">
<br />
</div>
<div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
<div id="hdMainContent">
<div id="numberOfCalls">All unsolved calls (<%=Html.ViewData("OpenCallCount")%>)</div>
<div id="unsolvedTix">
<div id="callHeaders">
<table id="callHeadersTbl" cellpadding="0" cellspacing="0">
<tr>
<td width="54"> </td>
<td width="270">Subject</td>
<td width="148">Logged</td>
<td width="58">Priority</td>
<td width="120">Updated</td>
</tr>
</table>
</div>
<div id="greyTicketBar"></div>
<table cellpadding="0" cellspacing="0" width="643">
<% For Each t As hdCall In ViewData.Model%>
<tr>
<td width="51" class="ticketList" align="center"><img src="/images/icons/<%=t.hdPriority.Priority%>.gif" /></td>
<td width="270" class="ticketList"><%=Html.ActionLink(t.Title, "Details", New With {.id = t.CallID})%></td>
<td width="148" class="ticketList"><%=t.loggedOn%></td>
<td width="58" class="ticketList"><%=t.hdPriority.Priority%></td>
<td width="115" class="ticketList"><%=t.updatedOn%></td>
</tr>
<% Next%>
</table>
</div>
</div>
<div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
</div>
<div id="hdSpacer"></div>
<div id="hdMenus">
<div id="browseBox">
<div id="blueTop"><img src="images/blueboxTop.gif" /></div>
<div id="blueContent">
<img src="images/browse.gif" alt="Browse" /><br /><br />
<ul>
<li> Calls for Topps<br /><br /></li>
<li> Calls for TCH<br /><br /></li>
</ul>
</div>
<div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
<br />
<div id="Dashboard">
<div id="blueTop"><img src="images/blueboxTop.gif" /></div>
<div id="blueContent"><img src="images/dashboard.gif" alt="Dashboard" /><br /><br />
<div id="storePercent"><%=Html.ViewData("OpenCallCount")%><br />
Calls Open</div>
<ul style="font-weight: bold;">
<li> Urgent: <%=Html.ViewData("UrgentCallCount")%><br /><br /></li>
<li> High: <%=Html.ViewData("HighCallCount")%><br /><br /></li>
<li> Normal: <%=Html.ViewData("NormalCallCount")%><br /><br /></li>
<li> Low: <%=Html.ViewData("LowCallCount")%></li>
</ul>
</div>
<div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
</div>
</div>
</asp:Content>
Now, the idea I have for it, even though I know it won't work would basically be:
'
' GET: /Calls/
<Authorize()> _
Function Index() As ActionResult
ViewData("OpenCallCount") = callRepository.CountOpenCalls.Count()
ViewData("UrgentCallCount") = callRepository.CountUrgentCalls.Count()
ViewData("HighCallCount") = callRepository.CountHighCalls.Count()
ViewData("NormalCallCount") = callRepository.CountNormalCalls.Count()
ViewData("LowCallCount") = callRepository.CountLowCalls.Count()
ViewData("MyOpenCallsCount") = callRepository.CountMyOpenCalls(Session("LoggedInUser")).Count()
ViewData("UserName") = Session("LoggedInUser")
Dim viewOpenCalls = callRepository.FindAllOpenCalls()
Dim viewMyOpenCalls = callRepository.FindAllMyCalls(Session("LoggedInUser"))
Return View(viewOpenCalls)
Return View(viewMyOpenCalls)
End Function
What I'm wondering is, what would be the correct way to do this? I haven't a clue as how to go about the right way, I think I at least have the theory there though, just not how to implement it.
Thanks for any help in advance.
EDIT
Based on the below comments, I have made the following code edits/additions:
Class Calls
Private _OpenCalls As hdCall
Public Property OpenCalls() As hdCall
Get
Return _OpenCalls
End Get
Set(ByVal value As hdCall)
_OpenCalls = value
End Set
End Property
Private _MyCalls As hdCall
Public Property MyCalls() As hdCall
Get
Return _MyCalls
End Get
Set(ByVal value As hdCall)
_MyCalls = value
End Set
End Property
End Class
Index() Action
'
' GET: /Calls/
<Authorize()> _
Function Index() As ActionResult
ViewData("OpenCallCount") = callRepository.CountOpenCalls.Count()
ViewData("UrgentCallCount") = callRepository.CountUrgentCalls.Count()
ViewData("HighCallCount") = callRepository.CountHighCalls.Count()
ViewData("NormalCallCount") = callRepository.CountNormalCalls.Count()
ViewData("LowCallCount") = callRepository.CountLowCalls.Count()
ViewData("MyOpenCallsCount") = callRepository.CountMyOpenCalls(Session("LoggedInUser")).Count()
ViewData("UserName") = Session("LoggedInUser")
Dim viewOpenCalls As New Calls With {.OpenCalls = callRepository.FindAllOpenCalls()}
Dim viewMyOpenCalls As New Calls With {.MyCalls = callRepository.FindAllMyCalls(Session("LoggedInUser"))}
Return View(New Calls())
End Function
Calls/Index.aspx
<%# Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Calls>" %>
<%# Import Namespace="CustomerServiceHelpdesk" %>
<asp:Content ID="ticketsHeader" ContentPlaceHolderID="TitleContent" runat="server">
Topps Customer Service Helpdesk - View All Calls
</asp:Content>
<asp:Content ID="ticketsContent" ContentPlaceHolderID="MainContent" runat="server">
<div id="hdMain">
<div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
<div id="hdMainContent">
<div id="numberOfCalls">Calls assigned to you (<%=Html.ViewData("MyOpenCallsCount")%>)</div>
<div id="assignedToMe">
<div id="callHeaders">
<table id="callHeadersTbl" cellpadding="0" cellspacing="0">
<tr>
<td width="54"> </td>
<td width="270">Subject</td>
<td width="148">Logged</td>
<td width="120">Updated</td>
</tr>
</table>
</div>
<div id="greyTicketBar"> Assignee: <strong><%=Html.ViewData("UserName")%></strong></div>
<table cellpadding="0" cellspacing="0" width="643">
<% For Each aT In Model.MyCalls%>
<tr>
<td width="54" class="ticketList"> </td>
<td width="270" class="ticketList"><%=Html.ActionLink(aT.Title, "Details", New With {.id = aT.CallID})%></td>
<td width="148" class="ticketList"><%=aT.loggedOn.Date.ToShortDateString%></td>
<td width="115" class="ticketList"><%=aT.updatedOn.Date.ToShortDateString%></td>
</tr>
<% Next%>
</table>
</div>
</div>
<div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
<div id="bigbreak">
<br />
</div>
<div id="hdMainTop"><img src="images/hdMainTop.gif" alt="" /></div>
<div id="hdMainContent">
<div id="numberOfCalls">All unsolved calls (<%=Html.ViewData("OpenCallCount")%>)</div>
<div id="unsolvedTix">
<div id="callHeaders">
<table id="callHeadersTbl" cellpadding="0" cellspacing="0">
<tr>
<td width="54"> </td>
<td width="270">Subject</td>
<td width="148">Logged</td>
<td width="58">Priority</td>
<td width="120">Updated</td>
</tr>
</table>
</div>
<div id="greyTicketBar"></div>
<table cellpadding="0" cellspacing="0" width="643">
<% For Each t As hdCall In Model.OpenCalls%>
<tr>
<td width="51" class="ticketList" align="center"><img src="/images/icons/<%=t.hdPriority.Priority%>.gif" /></td>
<td width="270" class="ticketList"><%=Html.ActionLink(t.Title, "Details", New With {.id = t.CallID})%></td>
<td width="148" class="ticketList"><%=t.loggedOn%></td>
<td width="58" class="ticketList"><%=t.hdPriority.Priority%></td>
<td width="115" class="ticketList"><%=t.updatedOn%></td>
</tr>
<% Next%>
</table>
</div>
</div>
<div id="hdMainBottom"><img src="images/hdMainBottom.gif" alt="" /></div>
</div>
<div id="hdSpacer"></div>
<div id="hdMenus">
<div id="browseBox">
<div id="blueTop"><img src="images/blueboxTop.gif" /></div>
<div id="blueContent">
<img src="images/browse.gif" alt="Browse" /><br /><br />
<ul>
<li> Calls for Topps<br /><br /></li>
<li> Calls for TCH<br /><br /></li>
</ul>
</div>
<div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
<br />
<div id="Dashboard">
<div id="blueTop"><img src="images/blueboxTop.gif" /></div>
<div id="blueContent"><img src="images/dashboard.gif" alt="Dashboard" /><br /><br />
<div id="storePercent"><%=Html.ViewData("OpenCallCount")%><br />
Calls Open</div>
<ul style="font-weight: bold;">
<li> Urgent: <%=Html.ViewData("UrgentCallCount")%><br /><br /></li>
<li> High: <%=Html.ViewData("HighCallCount")%><br /><br /></li>
<li> Normal: <%=Html.ViewData("NormalCallCount")%><br /><br /></li>
<li> Low: <%=Html.ViewData("LowCallCount")%></li>
</ul>
</div>
<div id="blueBottom"><img src="images/blueboxBottom.gif" /></div>
</div>
</div>
</asp:Content>
However, the line <%=Html.ViewData("MyOpenCallsCount")%> gives me an End of Statement expected error.
Also, I did get it to compile once, but I got the error Unable to cast object of type 'System.Data.Linq.DataQuery1[CustomerServiceHelpdesk.hdCall]' to type 'CustomerServiceHelpdesk.hdCall'. from the line Dim viewOpenCalls As New Calls With {.OpenCalls = callRepository.FindAllOpenCalls()}
Where did I go wrong?
I'm a bit of noob when it comes to things like this, so any help is much appreciated.
Well, you can't return two values from a function, if that's what you are trying to do (the title suggests that's what you want).
Plus that's not how http works anyway. There's always just one response for a request.
But if you are trying to send both your viewOpenCalls and viewMyOpenCalls objects to one view, then you can do that with making your view have a model that'll hold both of them.
Like this :
//My VB is a bit rusty so I'm writing this in C#
class Calls {
public yourDataType OpenCalls { get; set; }
public yourDataType MyCalls { get; set; }
}
In your controller action :
return View(new Calls { OpenCalls = viewOpenCalls, MyCalls = viewMyOpenCalls })
//I gues in VB it would be like this :
Dim viewOpenCalls = callRepository.FindAllOpenCalls()
Dim viewMyOpenCalls = callRepository.FindAllMyCalls(Session("LoggedInUser"))
Return View(New Calls _
With {.OpenCalls = viewOpenCalls, .MyCalls = viewMyOpenCalls})
In your view make sure it's model is type of the Calls class.
<%# Page Inherits="System.Web.Mvc.ViewPage<Calls>" %>
And now you can access the properties with <%=Model.OpenCalls %> and <%=Model.MyCalls %>
Once I'd worked out the correct way of doing it (a little while a go now) I got it working.
For reference, this is the way it should be done:
Public Class TheCalls
Private _OpenCalls As IQueryable(Of hdCall)
Public Property OpenCalls() As IQueryable(Of hdCall)
Get
Return _OpenCalls
End Get
Set(ByVal value As IQueryable(Of hdCall))
_OpenCalls = value
End Set
End Property
Private _AssignedCalls As IQueryable(Of hdCall)
Public Property AssignedCalls() As IQueryable(Of hdCall)
Get
Return _AssignedCalls
End Get
Set(ByVal value As IQueryable(Of hdCall))
_AssignedCalls = value
End Set
End Property
End Class
1) Create a ViewData class. This is just a POCO class with properties defined for each data item you want to show on the view (e.g. OpenCallCount)
2) Create a strongly typed view that uses this ViewData class.
3) Pass a new instance of your ViewData class, with the properties set, to your view.
This will help you avoid using magic strings everywhere (e.g. ViewData("OpenCallCount") = ... becomes myViewDataClass.OpenCallCount = ...)
You could probably tidy up the view by using two partial view classes, or making it slightly more generic, but it will do the job at the moment.

Resources