How to use Hidden fields in HTML5 asp.net mvc 4? - asp.net

I am working on a MVC 4 asp.net application(vb.net project)
<table>
<tr><td>Billable item</td><td>#Html.DropDownList("RteBillableItem") </td><td>Quantity</td><td>#Html.TextBox("Quantity")</td></tr>
<tr><td>Doctor Fee</td><td>#Html.TextBox("DoctorFee")</td><td>Hospital Fee</td>
<td>#Html.TextBox("HospitalFee")</td></tr>
<tr><td></td><td></td><td></td><td><input type="submit" id="AddItem" value="Add Item" /></td></tr>
</table>
As seen in the image, i will be selecting an item from the drop down list, enter some data in the text boxes, and click on the button "Add Item", the form will do a post back.
The code in post back controller is :
HttpPost()
Function Index(ByVal Collection As FormCollection, ByVal Hidden As HiddenField) As ActionResult
Dim Test As New TestModel
Test.Update(CodeFactory(item), CInt(Collection("Quantity")),CInt(Collection("DoctorFee")), CInt(Collection("HospitalFee")))
//This will add the Item in a dictionary which i have used in my ModelTest
Return View(Test)
End Function
After adding the Item into my dictionary, i will send this model to "View", where i have to list the values in a table, picking the values from Dictionary of TestModel. The code for listing the values in View is:
#For Each item In Model
Dim currentItem = item
#<tr>
<td>
#Html.DisplayFor(Function(modelItem) currentItem.BillableItemId)
</td>
<td>
#Html.DisplayFor(Function(modelItem) currentItem.BillableItemName)
</td>
<td>
#Html.ActionLink("Edit", "Edit", New With {.id = currentItem.OpBillId}) |
#Html.ActionLink("Details", "Details", New With {.id = currentItem.OpBillId}) |
#Html.ActionLink("Delete", "Delete", New With {.id = currentItem.OpBillId})
</td>
</tr>
next
</table>
But the thing is , whenever i do a post back, all the previous data which i have added in the dictionary of TestModel is lost. I know that i am re-initializing my Test object of the model, TestModel.
I think i have to use Hidden fields, to retain the old data of TestModel, even after PostBack.
But i do not know how to do it, please give your valuable suggestions.

Related

i don't know what #foreach in asp.net mvc 5 do

I'm a newbie and i don't know what #foreach do.
this is code written by my teacher
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.MaSach)
</td>
<td>
<img src="~/HinhAnhSP/#Html.DisplayFor(modelItem => item.AnhBia)" width="100" />
</td>
<td>
#Html.DisplayFor(modelItem => item.TenSach)
</td>
<td>
#Html.DisplayFor(modelItem=> item.GiaBan)
</td>
<td>
#Html.ActionLink("Details", "Details", new { id = item.MaSach }) |
#Html.ActionLink("Add to Cart", "Add", "Cart", new { id = item.MaSach }, new {url ="" })
</td>
</tr>
}
foreach is a construct that loops over the items of a collection (actually an enumeration, but never mind that now) and repeats whatever is inside the brackets ({,}) for each of the items, assigning a variable (named item in this case) on each iteration of the loop.
In your case, Model is a collection of items (of some type you haven't shown us). So on each iteration, it'll write all the HTML code between the brackets, assigning a variable named item on each iteration with the contents of the current item.
So imagine Model was a collection of five items of type MyType, which is defined as:
class MyType {
public string Name;
}
Where Name contains the string Hello1 in the first item, Hello2 in the second item, etc:
So if you do:
#foreach(var item in Model) {
<p>#(item.Name)</p>
}
The result once parsed will be:
<p>Hello1</p>
<p>Hello2</p>
<p>Hello3</p>
<p>Hello4</p>
<p>Hello5</p>

Redirecting from one value to another using action link

When I click on the column values in a table, it should redirect me to another table which has a common id, but a different view.
#foreach (var item in Model.Vendor)
{
<tr>
<td>
#Html.ActionLink(item.VendorName, "#item.Site Name", new { Id = item.VendorName})
</td>
<td>
#item.SiteCount
</td>
<td>
#item.LastReviewType
</td>
<td>
#item.LastReviewDate
</td>
<td>
#item.TeamLead
</td>
...
When I click on the vendorname values in the table, it should take me to the table which shows the sites which are assigned to the vendor.
Assuming you would have a controller by the name VendorController which has an action SiteList which would take argument venderId to generate the view, you could use
#Html.ActionLink(item.VendorName, “SiteList” /*Name of the action*/, “Vendor” /* Name of the controller */, new { venderId = item.VendorId } /* parameter */)
If the action is on the same controller which generated the first table, you may ignore /* Name of the controller argument */, thus
#Html.ActionLink(item.VendorName, “SiteList” /*Name of the action*/, new { venderId = item.VendorId } /* parameter */)
Hope this will help you

ASP.NET RAZOR cannot capture the event

I want to get the value from the selected radio button after a user clicked on one of them and display it at the bottom, but it doesn't work.
I do not want to use JAVASCRIPT and MVC.
Please help~~~
#{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Contact";
var selectedValue = "Male selected";
}
#{
if (IsPost)
{
selectedValue = Request["Gender"].ToString();
}
}
<div>
<table>
<tr>
<td>
#Html.RadioButton("Gender", "Male", true) Male
</td>
<td>
#Html.RadioButton("Gender", "Female", false) Female
</td>
</tr>
</table>
<b>#selectedValue</b>
</div>
If you don't want to use JavaScript, you will need a roundtrip to the server. Razor is not executed on the client. In fact the client never even sees the razor code in the first place.

mvc3 html.Dropdownlist() and html.beginform()

Hi guys I have a dropdownlist and a submit button in my view.
I want the user to be able to select an item in the dropdownlist that calls an action method in the controller that gets data from the database.
I also have a button and I want the user to be able to select a checkbox in a grid and click the submit button to pass the value of the checkbox to an action method in the controller.
The problem is when I select an item from the dropdownlist it calls the action method for the submit button "DiscontinueProduct" and
not the action method for the dropdownlist ("GetProductByID"), can someone please tell me what I'm doing wrong?
Here is a my code.
Thanks in advance.
=============
view
<div>
#Using Html.BeginForm("GetProductByID", "Product")
#Html.DropDownList("CategoryID", DirectCast(ViewData("Categories"), SelectList), " -- Choose One -- ", New With {Key .onchange = "$('form').submit();"})
End Using
</div>
#Using Html.BeginForm("DiscontinueProduct", "Product")
#<text>
<table>
<tr>
<th></th>
<th>ProductName</th>
<th>SupplierID</th>
<th>CategoryID</th>
<th>Discontinued</th>
</tr>
#For Each item In Model
#<tr>
<td>
#Html.ActionLink("Edit", "Edit", New With {.id = item.ProductID}) |
#Html.ActionLink("Details", "Details", New With {.id = item.ProductID}) |
#Html.ActionLink("Delete", "Delete", New With {.id = item.ProductID})
</td>
<td>#item.ProductName</td>
<td>#item.SupplierID</td>
<td>#item.CategoryID
<input type="checkbox" name="task" id="isTaskSelected" value=" #item.CategoryID.ToString() " />
</td>
<td>#item.Discontinued</td>
</tr>
Next
</table>
<div id="btncomplete" style="display: none">
<input type="submit" value="Discontinue" />
</div>
</text>
End Using
=====================
Controller
Function GetProductByID(ByVal id As Integer) As ActionResult
Dim cquery2 = From product In db.Products
Where product.CategoryID = id
viewmodel.ProductList = cquery2.ToList()
Return PartialView("Products", viewmodel.ProductList)
Return PartialView("Products", viewmodel.ProductList)
End Function
<HttpPost()> _
Function DiscontinueProduct(ByVal collection As FormCollection) As ActionResult
Try
' Code to update product field as discontinue.
Return RedirectToAction("Index")
Catch
Return View()
End Try
End Function
It seems that you have to prevent the SUBMIT action for your drop-down list. Have a look here How to prevent buttons from submitting forms

ASP.Net MVC EditorFor not working

I am trying to render a table, using EditorFor, and a partialview, I think.
I have a model with a List<> property defined like this:
public List<TransactionSplitLine> TransactionSplitLines { get; set; }
The idea is that a user selects a few drop downs and enters a value into an edit box, and clicks a button. The model goes back to the controller, and the controller adds the entered values to the List<>
[HttpPost]
public ActionResult AccountTransaction(AccountTransactionView model)
{
var reply = CreateModel(model);
if (model.CategoryIds != null)
{
foreach (var c in model.CategoryIds)
{
reply.TransactionSplitLines.Add(new TransactionSplitLine { Amount = "100", Category = "Test Category", SubCategory = "Test More", CategoryId = int.Parse(c) });
}
}
reply.TransactionSplitLines.Add(new TransactionSplitLine { Amount = "100", Category = "Test Category", SubCategory = "Test More", CategoryId = 1 });
return View("AccountTransaction", reply);
}
Ignore the CreateModel. It simply sets up some data. Also, I am hardcoding data. This will eventually come from some form values.
The model is then returned to the same screen, allowing the user to ender more data. Any items in the List<> are read and a Table is rendered. I also have to store the current listen item values in hidden fields, so that they can be submitted back, along with the new data entered, so that the list can grow each time the user adds data.
The view is defined like this:
<table width="600">
<thead>
<tr class="headerRow">
<td>
Category
</td>
<td>
Sub Category
</td>
<td>
Amount
</td>
</tr>
</thead>
<tbody>
<%=Html.EditorFor(m=>m.TransactionSplitLines) %>
</tbody>
</table>
This is my first attempt with EditorFor...
My View is in a folder 'Views/BankAccount/AccountTransaction.aspx
I have created a ascx in Views/Shared/TransactionSplitLines.ascx
The code for the ascx is like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BudgieMoneySite.Models.TransactionSplitLine>" %>
<tr>
<td>
<%=Model.Category %>
<%=Html.HiddenFor(x => x.CategoryId)%>
</td>
<td>
<%=Model.SubCategory %>
<%=Html.HiddenFor(x => x.SubCategoryId)%>
</td>
<td>
<%=Model.Amount %>
<%=Html.HiddenFor(x => x.AmountValue)%>
</td>
</tr>
This is data
The 'This is data' is just test stuff, which is never rendered.
When I run this, all that happens is that my output is rendered as:
<table width="600">
<thead>
<tr class="headerRow">
<td>
Category
</td>
<td>
Sub Category
</td>
<td>
Amount
</td>
</tr>
</thead>
<tbody>
Test Category
</tbody>
</table>
It seems like the ascx isn't being used? I'd expect to see the 'This is data' text. But, nothing. Hopefully you can see an obvious fault?
Your editor template should be either:
~/Views/Shared/EditorTemplates/TransactionSplitLine.ascx
or:
~/Views/BankAccount/EditorTemplates/TransactionSplitLine.ascx
The name of the ascx is always the type name of the collection item (TransactionSplitLine and not TransactionSplitLines) and it should be situated at ~/Views/Shared/EditorTemplates or ~Views/ControllerName/EditorTemplates.
Or if you want to use a custom editor template name:
<%= Html.EditorFor(m=>m.TransactionSplitLines, "~/Views/foo.ascx") %>
Or use UIHintAttribute on your model.

Resources