ASp.NET MVC2 - Rendering an action link and text on same line - asp.net

I will like to achieve the following html using Html.ActionLink:
<li>John DoePresident</li>
The name "John Doe" and title "President" will be coming from a staff model. This is what I have:
<% foreach (var item in Model as IEnumerable<AkwiMemorial.Models.Staff>)
{ %>
<li><%= Html.ActionLink(item.Name, "GetStaffDetails", "WhatWeDo", new { staffID = item.Id }, null) %> item.Position</li>
<% } %>
Instead of rendering "item.Position" literally, I will like this string extracted from the model.
TIA.

Try this:
<% foreach (var item in Model as IEnumerable<AkwiMemorial.Models.Staff>)
{ %>
<li>
<%= Html.ActionLink(item.Name, "GetStaffDetails", "WhatWeDo", new { staffID = item.Id }, null) %>
<%=item.Position%>
</li>
<% } %>

Related

Remove Link from item in ASP.NET

Pretty new to ASP.NET and trying to figure out how to have the code just echo the information it fetched.
<ul class="accordion">
<% foreach (var cat in Model.Categories.Where(x=>x.ParentId==null).OrderBy(x=>x.DisplayOrder).ThenBy(x=>x.CategoryName)) { %>
<li style="height: auto; min-height:30px;">
<%= Html.ActionLink(cat.CategoryName, "Index", "Products", new { id=cat.UniqueName, area="" }, null)%>
<%if(cat.SubCategories.Count>0) {%>
<ul class="sub-menu" style="font-size:.8em; padding-left:20px;">
<% foreach (var c in cat.SubCategories) { %>
<li><%= Html.ActionLink(c.CategoryName, "Index", "Products", new { id=c.UniqueName, area="" }, null)%></li>
<% } %>
</ul>
<%}%>
</li>
<%} %>
</ul>
Currently I know it has to do with <%= Html.ActionLink(cat.CategoryName, "Index", "Products", new { id=cat.UniqueName, area="" }, null)%> that line but not sure how to make it pass the information. Might be a stupid question but Google definitely did not help when searching for this answer.
Thanks.
Here is a brief overview of some of the commands:
<%= Html.ActionLink(cat.CategoryName, "Index", "Products", new { id=cat.UniqueName, area="" }, null)%>
Will create:
{CategoryName}
Which can also be constructued as:
<%: cat.CategoryName%>
Which will also create:
{CategoryName}
So, you can output just the name or ID using:
<%: cat.CategoryName %>
OR
<%= cat.CategoryName %>
<%= is "Response.Write" and "<%:" Is Response.Write with HTML Encode

ActionLink routeValue from a TextBox

I'm working on the following:
1- The user enters a value inside a textBox.
2- then clicks edit to go to the edit view.
This is my code:
<%= Html.TextBox("Name") %>
<%: Html.ActionLink("Edit", "Edit")%>
The problem is I can't figure out how to take the value from the textBox and pass it to the ActionLink, can you help me?
You can't unless you use javascript. A better way to achieve this would be to use a form instead of an ActionLink:
<% using (Html.BeginForm("Edit", "SomeController")) { %>
<%= Html.TextBox("Name") %>
<input type="submit" value="Edit" />
<% } %>
which will automatically send the value entered by the user in the textbox to the controller action:
[HttpPost]
public ActionResult Edit(string name)
{
...
}
And if you wanted to use an ActionLink here's how you could setup a javascript function which will send the value:
<%= Html.TextBox("Name") %>
<%= Html.ActionLink("Edit", "Edit", null, new { id = "edit" })%>
and then:
$(function() {
$('#edit').click(function() {
var name = $('#Name').val();
this.href = this.href + '?name=' + encodeURIComponent(name);
});
});

Form input in a foreach loop returns empty model

I have a list object for which I tried to display text boxes in a foreach loop. However the post returns empty object. I couldn't see the cause.
Here is the code in the view
<%using (Html.BeginForm("makeTransfer", "shareTransfer")) { %>
<% foreach (var i in Model.Inform)//int i = 0; i < Model.Inform.Count(); i++){ %>
<%:Html.HiddenFor(x=>i.shares, new{#value = i.shares}) %>
...
<td style = "width:20px"><%:Html.TextBoxFor(x=>i.sharesRq)%></td> cuddling
<%} %>
<%:Html.HiddenFor(x => x.accSrc, new { #value = Model.accSrc })%>
<%:Html.HiddenFor(x=>x.accDst, new{ #value = Model.accDst}) %>
Date of Transfer<%:Html.TextBoxFor(x => x.date)%>
Transfer with benefit<%:Html.CheckBoxFor(x => x.withBenefit)%>
<input type="submit" name="save" value="Save" /></div>
<input type="submit" name="cancel" value="Cancel" /></div>
<%} %>
And Here is the controller
public ActionResult makeTransfer(vmTransfer transfer, string save, string cancel)
{
if (cancel != null)
return RedirectToAction("startTransfer");
else if (save != null)
{
foreach (var t in transfer.Inform)
{ ...
My problem is, transfer.Inform( 2nd line from the last) which is a list is empty when the form posts. Any help please, ASAP.
I would recommend you using editor templates instead of writing any loops in your views:
<% using (Html.BeginForm("makeTransfer", "shareTransfer")) { %>
<%= Html.EditorFor(x => x.Inform) %>
<%= Html.HiddenFor(x => x.accSrc, new { #value = Model.accSrc }) %>
<%= Html.HiddenFor(x => x.accDst, new { #value = Model.accDst }) %>
Date of Transfer <%= Html.TextBoxFor(x => x.date) %>
Transfer with benefit <%= Html.CheckBoxFor(x => x.withBenefit) %>
<input type="submit" name="save" value="Save" /></div>
<input type="submit" name="cancel" value="Cancel" /></div>
<% } %>
and in the corresponding editor template (~/Views/Shared/EditorTemplates/InformViewModel.ascx):
<%# Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.InformViewModel>"
%>
<%= Html.HiddenFor(x => x.shares) %>
...
<td style="width:20px">
<%= Html.TextBoxFor(x => x.sharesRq) %>
</td>
Remark: you might need to adjust the name of the editor template based on the type of the Inform property.
Editor templates will take care of generating proper id and names of the input fields so that everything binds correctly:
[HttpPost]
public ActionResult makeTransfer(vmTransfer transfer, string save, string cancel)
{
if (cancel != null)
{
return RedirectToAction("startTransfer");
}
else if (save != null)
{
foreach (var t in transfer.Inform)
{
...
}
}
...
}

HTML 'id' attribute for items in dropdown made from html helper

Can we set an id attribute as I would for something like a table column via:
for an html dropdown list element that is created with a helper such as:
<% for (int i = 0; i < Model.Trx.TransactionHolidayCityCollection.Count; i++)
{%>
<%= i > 0 ? "," : "" %>
<%= DropDownData.HolidayDays().ToList().Find(item => item.Value == Model.Trx.TransactionHolidayCityCollection[i].HolidayCityID.Value.ToString()).Text %>
<%} %>
You don't need a helper, necessarily:
<select id="holidayCities" name="holidayCities">
<% foreach (HolidayCity city in Model.Trx.TransactionHolidayCityCollection) { %>
<option
value="<%=city.HolidayCityID.Value %>"
id="holidayCity_<%=city.HolidayCityID.Value %>"
><%=city.Name %></option>
<% } %>
</select>
If you wanted to use the HtmlHelper you can write:
<%=Html.DropDownList("holidayCities", Model.HolidayCitiesSelectList) %>
Model.HolidayCitiesSelectList should be of type IEnumerable<SelectListItem>
This type of customization isn't avaiable for the built in helpers so you might end up creating your own Html.DropDownList helper method.
http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem(v=vs.98).aspx

Why are my text fields getting wiped out?

I've got a partial that is called by Ajax to add new line items. If I type in some data in the part ID field then click the link to Add another item, it wipes out what I just typed.
View:
<label>Parts Used
<%= Ajax.ActionLink("Add another item", "BlankEditor", new AjaxOptions {
UpdateTargetId = "partusageitems", InsertionMode = InsertionMode.InsertAfter}) %>
</label>
<div id="partusageitems">
<% foreach (var part in Model.PartUsage)
{
Html.RenderPartial("~/Views/Fsr/_PartsUsage.ascx", part);
}%>
</div>
Partial:
<div>
<% var fieldPrefix = "PartUsage[]."; %>
Part ID: <%= Html.TextBox(fieldPrefix + "ID", Model.ID, new { size = "25"})%>
Serial Number: <%= Html.TextBox(fieldPrefix + "Serial", Model.Serial, new { size = "25" })%>
Quantity: <%= Html.TextBox(fieldPrefix + "Quantity", Model.Quantity, new { size = "10"}) %>
Delete
</div>
Controller:
public ActionResult BlankEditor()
{
return View("_PartsUsage", new Part());
}
This may just be a typo, but Partial is missing a DIV closing tag at the end.

Resources