Why are my text fields getting wiped out? - asp.net

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.

Related

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)
{
...
}
}
...
}

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

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>
<% } %>

Use textbox value on submit as a query string variable

How would I take a text box value and use it in the query string on submit? I'd like it to start as this,
/News?favorites=True
and end up something like this after the user enters in a search and clicks search.
/News?query=test&favorites=True
The controller action looks like this
public ActionResult Index(string query,bool favorites)
{
//search code
}
This question is something close to what I'd like to do, but I'd like to use the query string and maintain the existing values in the query string.
Thanks.
Two possibilities:
place the textbox inside a <form> with method="GET"
use javascript to read the value and pass it to the server (with AJAX or window.location to perform a redirect)
Example with a <form>:
<% using (Html.BeginForm("index", "news", FormMethod.Get)) { %>
<label for="query">Query:</label>
<%= Html.TextBox("query") %>
<input type="submit" value="Search" />
<% } %>
Example with javascript:
<label for="query">Query:</label>
<%= Html.TextBox("query") %>
<%= Html.ActionLink("Search", "index", "news", new { id = "search" }) %>
and then in a separate js file:
$(function() {
$('#search').click(function() {
var query = $('#query').val();
// Here you could use AJAX instead of window.location if you wish
window.location = this.href + '?query=' + encodeURIComponent(query);
return false;
});
});
Using Darin's above <form> answer but with Razor markup:
#using (Html.BeginForm("index", "news", FormMethod.Get))
{
<label for="query">Search:</label>
#Html.TextBox("query")
<input type="submit" value="Search" />
}

Ajax.Actionlink, how to get the form data to the controller action

View
<%= Ajax.ActionLink("Create", "Create", ViewData.Model, new AjaxOptions { HttpMethod = "POST" })%>
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Ajax.BeginForm("Create", "Customer", ViewData.Model, new AjaxOptions { HttpMethod ="POST" }))
{%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Title">Title:</label>
<%= Html.TextBox("Name")%>
<%= Html.ValidationMessage("Name", "*")%>
</p>
<p>
<label for="Description">Description:</label>
<%= Html.TextArea("ContactNo")%>
<%= Html.ValidationMessage("Name", "*")%>
</p>
</fieldset>
<% } %>
Controller
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Customer info)
{
//INFO IS NULL???
//WHAT IS THE PROBLEM?
}
You can't pass the model object. This argument expects the route values such as an ID.
if you pass in Ajax.ActionLink("Create", "Create", new { id=23 }, ....
it will create /create/23.

Resources