Is it possible to return the ActionLink html from a ViewModel? (MVC3 + Razor) - asp.net-mvc-3

I have a four viewmodels/views which display similar but different data.
Is it possible to have a property on the viewmodel which retuns ActionLinks (or the html for them ?)
e.g.
At the moment on each of my Views I have
<table>
<tr>
<td>#Html.ActionLink("My Open Calls", "MyOpenCalls")</td>
<td>#Html.ActionLink("All Open Calls", "AllOpenCalls")</td>
<td>#Html.ActionLink("My Calls Today", "MyCallsToday")</td>
<td>#ViewBag.Title</td>
</tr>
</table>
but would it be possible to have:
<table>
<tr>
#Model.MenuHtml
</tr>
</table>

While it might be possible to store HTML in view models properties I don't think it would be a good idea. If you want to reuse some code why not simply put this table into a partial and then include the partial:
#Html.Partial("_Links")
Another possibility is to use a custom HTML helper that will generate those links.

Related

Add element to existing html using specific location using id and class using prototype insert

i am trying to add Element to existing html content using prototype insert. below is the html on which i am working
<table id="productGrid_table" class="data" cellspacing="0">
<thead>
<tr class="filter">
<th>
<div class="range">
</div>
</th>
</tr>
</thead>
here i want to add another < th> in < tr class="filter">, i am trying to achieve this using below prototype code
$('filter').insert("<th><div class='field-100'></div></th>");
please provide some suggestion what i am doing wrong here???
In PrototypeJS $('filter') selects element with id="filter". If you want to select by class, then use $$ function. Something like this:
$$('.filter')[0].insert("<th><div class='field-100'></div></th>");
But I'd recommend either changing class name to something more unique, or use id instead of class.

Unable to reference all Model objects in MVC View

I have an odd problem whereby the model in my vbhtml file will correctly reference and bring up the model's attributes for 1 item but not for another.
FYI, The page is added as a partial page, connected to another vbhtml page.
My page is structured such that I want to list a number of people (beneficiaries), below each question (question text).
I am referencing my Model type using the syntax: #ModelType RHEAL_START.QuestionWithAnswers, so this should automatically allow the model items to be declared when instantiated.
The #ModelType declaration correctly allows the referencing of the 2nd model field (Model.benAnswers) but not the first one I have declared (Model.questionText).
Both items are part of the same model and, I have tried moving the declarations above and below the and headers but, this did not bring up an IntelliSense for the Model.questionText.
I looked at the following post and tried addding in a reference to the Layout file but alas, this did not bring up an IntelliSense either.
MVC3 - render view that is not a method in a controller
See the code below. Can anyone suggest where I'm going wrong and what else I can try?
If you need more information, please let me know.
#ModelType RHEAL_START.QuestionWithAnswers
#Code
Layout = "~/Views/MedicalQuestions/MedicalQuestions.vbhtml"
End Code
<div id="questionPanel">
#Code
Dim qAndARef As Integer = 0
End Code
<!-- Show question text and number -->
<h1>Model.questionText</h1>
<table>
<tr>
<td style="font-weight: bold">
Beneficiary </td>
<td style="font-weight: bold">
Yes/No </td>
<td style="font-weight: bold">
Details </td>
</tr>
<!-- !!Iterating over each beneficiary for Medical Question -->
#For Each benanswer As RHEAL_START.BeneficiaryAnswer In Model.benAnswers
Html.Partial("../MedicalQuestions/BeneficiaryAnswerPartial", benanswer)
Next
</table>
#*#Html.ValidationSummary()*#
</div>
#Section Scripts
#Scripts.Render("~/Scripts/RHEAL/YesNoDropDownValidation.js")
End Section
I'm not that familiar with the VB syntax for Razor, but it looks like you're missing a # sign in front of #Model.questionText

#Html.RenderPartial causing #Html.CheckBoxFor to uncheck values

I have a partial view that I'm currently rendering and everything loads up nicely. I also have a #Html.Checkboxfor that iterates through a collection of items and enables a check for the controller when the value is 'True'. My problem is that the #Html.RenderPartial() seems to be unchecking all the values once it loads. I've checked the model and the values are there, if I remove #Html.RenderPartial() for my partial view then the #Html.CheckBoxFor() will display the checked values.
I'd like to ask the SO community if there is a fix for something like this as I've tried other solutions, but nothing seems to work.
Thanks in advance!
<fieldset>
<legend>Organization Access</legend>
<table>
<tr>
#{Html.RenderPartial("_OrganizationAccess");}
</tr>
</table>
</fieldset>
<fieldset>
<legend>Alerts</legend>
<table>
#for (var i = 0; i < Model.AlertMembership.Count; i++)
{
<tr>
<td>#Html.CheckBoxFor(p => p.AlertMembership[i].AlertStatus)</td>
<td>#Html.LabelForModel(Model.AlertMembership[i].AlertName)</td>
<td>#Html.HiddenFor(p => p.AlertMembership[i].AlertId)</td>
</tr>
}
</table>
</fieldset>
Found out what the problem was partial view's when rendered using (#HtmlRenderPartial("view here")) will load not only the content, but also affect the other DOM objects/attributes on the view page. I traced the problem down to an issue where the inputs where being checked to false. This is why the #Html.CheckBoxFor was showing 'false" for the checked attribute after the partial view rendered.

MVC3 Razor weakly typed view?

I know this sound somewhat off-piste but how would you create a weakly typed view where you pass in a collection of objects and iterate in razor accordingly and display in a table?
-- Controller View --
???
-- Razor View ---
#foreach (var item in Model)
{
<tr>
<td>
#item.attr1
</td>
<td>
#item.attr2
</td>
</tr>
}
Frist you know the data send
From Controller -------> view by two way
By weak type view
and by strong type view
there is no other way of passing data from controller to view ...(remember)
what is intelliscence ----> which show the related sub property of any model
like we write Model. --------> then all property show in
droupdown list after dot(.).
A.what is weak type view
This is used without using model i.e like using ViewBag and other.
There is no intellisence for this type of view and it is complicated, and when you write
any name which not exist then it give at runtime error.
Ex.
.............Controller
ViewBag.List = List<job>;
return View();
.............Razor View
#foreach(var item in ViewBag.List)
{
// when you write no intellisence and you want to write your own correct one...
#item.
}
B. What strongly type view
this is used model to send data from controller to view an vice-versa.
Model are strongly typed to view so, it show intellicence and when you write wrong
then there only error show at compile time..
Ex.
.................Controller
List<job> jobdata =new List<job>();
return View(jobdata);
................view
//Mention here datatype that you want to strongly type using **#model**
#model List<job>
#foreach(var item in Model)
//this **Model** represent the model that passed from controller
// default you not change
{
#item. //then intellisence is come and no need write own ....
}
that is weak and strong type view ......
So now you solve any problem with this basic.....
may I hope it help u....
But it is best to use Strongly Typed view so it become easy
to use and best compare to weak...
#model dynamic
Will do what you want, I believe.
If its going to be a collection, then maybe use
#model ICollection
It's not weakly typed. It's typed to a collection of some kind.
#model IEnumerable<MyClass>
OK, late to the party I know, but what you're after is a view stating "#model dynamic" as already stated.
Working Example: (in mvc3 at time of posting)
NB In my case below, the view is actually being passed a System.Collections.IEnumerable
As it's weakly typed, you will not get intelesense for the items such as #item.Category etc..
#model dynamic
#using (Html.BeginForm())
{
<table class="tableRowHover">
<thead>
<tr>
<th>Row</th>
<th>Category</th>
<th>Description</th>
<th>Sales Price</th>
</tr>
</thead>
#{int counter = 1;}
#foreach (var item in Model)
{
<tbody>
<tr>
<td>
#Html.Raw(counter.ToString())
</td>
<td>
#item.Category
</td>
<td>
#item.Description
</td>
<td>
#item.Price
</td>
</tr>
#{ counter = counter +1;}
</tbody>
}
</table>
}
Edit: removed some css

Why pass data context class through View() instead of ViewBag?

If I have a Controller, with an Index page:
#model IEnumerable<MvcMovie.Models.Movie>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table>
<tr>
<th>
Title
</th>
....
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
....
</tr>
}
</table>
Whats the advantage of using #model IEnumerable<Movie>? Why couldn't I just define ViewBag.Movies in the Controller and use that in the index page?
The advantage of using #model IEnumerable<Movie> is the clarity of saying right upfront - this view is meant to display (an) IEnumerable<Movie>. It may use some additional information (which will be stored in the ViewBag), but IEnumerable<Movie> is what it's meant to display. That's conceptually the reason, which has a lot to do with the concept of the MVC design pattern. And of course there are the technical reasons as #Tridus said.
Strictly speaking, you could. But you're not going to get the benefit of Intellisense or a straightforward "this is the wrong type" error if you pass in something else to the View. You also won't get errors caught if you enable compiling your views at compile time (though that's off by default so it's less of a benefit).

Resources