Access Html.ActionLink property in Controller file - asp.net-mvc-3

I need to access practiceid property in my controller file please help how I can access this
Corporate
Tax & Employee Comp/Exec Benefits
Business Finance & Restructuring
Regulatory
My csHtml code is as follows:
#foreach (var facetData in Model.SearchResultItems.Facets.Select((x) => new { Item = x.Key, Index = x.Value }))
{
<tr>
<td>
#Html.ActionLink(#facetData.Index, "Search","Search", new { practiceid = #facetData.Item })
</td>
</tr>
}
and controller code is as follows:
public ActionResult Search(string practiceid)
{
}
But I'm not getting anything in my practiceid parameter.

See this question.
What you want to do is to add parameters to the tag, not to the link. Which means you should leave fourth argument null and use fifth instead. ie.
#Html.ActionLink(#facetData.Index, "Search","Search", null, new { practiceid = facetData.Item })
And the documentation for this helper method with focus on fifth parameter.

Add the extra null
#foreach (var facetData in Model.SearchResultItems.Facets.Select((x) => new { Item = x.Key, Index = x.Value }))
{
<tr>
<td>
#Html.ActionLink(#facetData.Index, "Search","Search", new { practiceid = facetData.Item }, null)
</td>
</tr>
}

Related

Knockout.js code seems to be working intermittently

We use Knockout.js and MVC in a Webapp and are having an issue, where the order merchandise total observed is 0 by Knockout.js, as displayed in the GUI.
The code seems to be working intermittently, because sometimes we do show the merchandise total in other orders. All orders use the same code and a merchandise total non zero value is always seen in the
OrderDetailClientViewModel object within the controller before being passed to the view. The view is coded in Knockout.js and plain javascript.
The "Totals" property shown below is an object (Models.OrderTotalViewModel), which contains the "MerchandiseTotal" field, which is sometimes showing a 0. I'm showing the relevant parts of the two view files below. I don't see the merchandiseTotal value being modified erroneously in the code. Only helpful segments are shown below.
Any suggestions to fix this intermittent behavior would be greatly appreciated!!!
Thank you,
Ken
--Edit.cshtml
#model Models.OrderEditPageViewModel
...
var rawViewModel = #(Html.ToJson(Model.OrderDetailClientViewModel()));
var viewModel = ko.mapping.fromJS(rawViewModel,
{
'HeaderData': {
create: function(options) {
options.data.StartDate = new Date(options.data.StartDate);
options.data.EndDate = new Date(options.data.EndDate);
var headerData = ko.mapping.fromJS(options.data);
headerData.errors = new HeaderDataError();
return headerData;
}
},
'Totals': {
create: function(options) {
var totals = ko.mapping.fromJS(options.data);
totals.errors = new TotalsError();
totals.isPromoCodeApplied = ko.observable(false);
if ($.trim(totals.PromoCode()) !== "")
totals.isPromoCodeApplied(true);
return totals;
}
}
});
...
-- This segment in Edit.cshtml shows where the OrderTotalViewModel.cshtml is called for Totals.
<tr>
<td colspan="3">
#Html.EditorFor(m => m.Totals)
</td>
</tr>
--OrderTotalViewModel.cshtml
#model Models.OrderTotalViewModel
#{ Layout = null; }
...
<td colspan="3" style="text-align:right">
<table class="PaddedTable" style="width:925px">
<tbody>
<tr>
<td style="width:125px;text-align:right">Merchandise Total:</td>
<td style="text-align:right" class="NormalTextBold">
#(Html.Knockout().SpanFor(m => m.MerchandiseTotal)
.HtmlAttributes(new { #class = "NormalTextBold" })
.Currency())

DropDownList getting more than one values in MVC3

I am working on a membership app created in MVC3. I have two different kinds of members Organisation and Individual. After they paying the membership fee, I need to track their payment history. Thus, in the first row (the field after text Member), I need to display the "OrganisationName" of Organisation and "MemberForname" of Individual in DropDownList. But now I can only display either "OrganisationName" or "MemberForname". I tried a lot, but still can't figure it out by displaying both. Any ideas? Thanks in advance.[The screen shot above only displays "OrganisationName", and the bottom one only displays "MemberForname"].
<td>
Member
</td>
<td>
#Html.DropDownList("member", ((IEnumerable<ProActiveMembership.Areas.Members.Models.Member>)ViewBag.PossibleMembers).Select(option => new SelectListItem
{
Text = Html.DisplayTextFor(_ => option.Forename).ToString(),
//Text = Html.DisplayTextFor(_ => option.OrganisationName).ToString(),
Value = option.MemberID.ToString(),
Selected = (Model != null) && (option.MemberID == Model.MemberID)
}), "Choose...", new { id = "ddl" })
#Html.ValidationMessageFor(model => model.MemberID)
</td>
You can set the Text property like this: Text = string.Format("{0} {1}", option.Forename, option.OrganisationName)
Your code would then look like this:
<td>
Member
</td>
<td>
#Html.DropDownList("member", ((IEnumerable<ProActiveMembership.Areas.Members.Models.Member>)ViewBag.PossibleMembers).Select(option => new SelectListItem
{
Text = string.Format("{0} {1}", option.Forename, option.OrganisationName),
Value = option.MemberID.ToString(),
Selected = (Model != null) && (option.MemberID == Model.MemberID)
}), "Choose...", new { id = "ddl" })
#Html.ValidationMessageFor(model => model.MemberID)
</td>
As andreister suggests you can also add a member to ProActiveMembership.Areas.Members.Models.Member that returns the full name. Something like this:
public string FullName { get { return string.Format("{0} {1}", Forename, OrganisationName); } }
You can then use the new member like this:
Text = option.FullName

the razor view is not displaying a navigation property value

i have the following action method:-
[HttpPost]
public ActionResult Create(int questionid, Answer a)
{
if (ModelState.IsValid)
{
repository.AddAnswer(a);
repository.Save();
return PartialView("_details",a);
}
return View(a);
and the folloiwng _details partila view:-
<td>
#Html.DisplayFor(modelItem => Model.Description)
</td>
<td>
#Html.DisplayFor(modelItem => Model.Answer_Description.description)
</td>
<td>
#Ajax.ActionLink("Delete", "Delete", "Answer",
new { id = Model.AnswersID },
new AjaxOptions
{
Confirm = "Are You sure You want to delete this Answer ?",
HttpMethod = "Post",
UpdateTargetId = Model.AnswersID.ToString()
})
</td>
</tr>
the problem that i am facing is that the #Html.DisplayFor(modelItem => Model.Answer_Description.description) value is not being displayed automatically after an ajx call unless i refresh the web page., so what might be the problme?
I would suspect a lazy loading problem (hard to be sure with the infos you give, but)...
As Model.Description seems to be a primitive (string) Property, but Answer_Description.description is a primitive property of a navigation property.
Are you sure that the Answer_Description is correctly loaded ?
Edit : to be more precise : your action receives an Answer : does it have the Answer_Description.description when passed ?
If no, you should get the Answer_Description from database before returning View...

if else statement in Razor is not functioning

I am using an if else in Razor view to check for null value like this:
#foreach (var item in Model)
{
<tr id="#(item.ShopListID)">
<td class="shoptablename">#Html.DisplayFor(modelItem => item.Name)
</td>
<td class="shoptableamount">
#if (item.Amount == null)
{
Html.Display("--");
}
else
{
String.Format("{0:0.##}", item.Amount);
}
</td>
</tr>
}
However, no matter my model amount is null or having a value, the html rendered do not contain any value in the amount.
I wonder why this is happening. Any idea?
Thanks...
EDIT:
Decided to did it in controller:
// Function to return shop list food item amount
public string GetItemAmount(int fid)
{
string output = "";
// Select the item based on shoplistfoodid
var shopListFood = dbEntities.SHOPLISTFOODs.Single(s => s.ShopListFoodID == fid);
if (shopListFood.Amount == null)
{
output = "--";
}
else
{
output = String.Format("{0:0.##}", shopListFood.Amount);
}
return output;
}
and call at View like:
<td class="shoptableamount">
#Html.Action("GetItemAmount", "Shop", new { fid = item.ShopListFoodID })
</td>
You have to use the #()
#if (item.Amount == null)
{
#("--");
}
else
{
#String.Format("{0:0.##}", item.Amount)
}
As noted in the comments and other answers, the Html.Display is not for displaying strings, but for displaying data from the ViewData Dictionary or from a Model. Read http://msdn.microsoft.com/en-us/library/ee310174%28v=VS.98%29.aspx#Y0
I think you want to display "-----" if amount is null.
#foreach (var item in Model)
{
<tr id="#(item.ShopListID)">
<td class="shoptablename">#Html.DisplayFor(modelItem => item.Name)
</td>
<td class="shoptableamount">
#if (item.Amount == null)
{
#Html.Raw("--")
}
else
{
String.Format("{0:0.##}", item.Amount);
}
</td>
</tr>
}
That's because you are using the Display() method incorrectly. The overload you are using is Display(HtmlHelper, String). If you are looking for "--" to be the text, you should use something like:
#Html.Label("--");
There are actually two other ways to display text from a code block in razor besides the suggested #(""), using a <text> tag and it's shorthand #:
#{
#("--")
<text>--</text>
#:--
}
The code above will display -- three times.

How can I reference MVC item/model in HTMLAttributes?

I'm trying to list two radio buttons in each row of a table, but I haven't been able to assign unique IDs to each radio button. I'd like to assign the IDs based on the #item.myID as follows:
#foreach (var item in Model)
{
<tr>
<td>
#Html.RadioButton("Yes", #item.myID, #item.IsCool, new { id = "#item.myID", autopostback = "true" })
#Html.RadioButton("No", #item.myID, !#item.IsCool, new { id = "#item.myID", autopostback = "true" })
</td>
</tr>
}
However, the IDs keep rendering literally as "#item.myID". In other words, it's not treating the # sign as a special character. I've also tried using parenthesis, like this: "#(item.myID)".
You need to remove the quotes from around the id assignment, like so;
#foreach (var item in Model)
{
<tr>
<td>
#Html.RadioButton("Yes", item.myID, item.IsCool, new { id = item.myID, autopostback = "true" })
#Html.RadioButton("No", item.myID, !item.IsCool, new { id = item.myID, autopostback = "true" })
</td>
</tr>
}
Also, note that you don't need the additional "#" symbol when you are already in the context of another Razor code block - the Razor View Engine is pretty clever :)

Resources