MVC :: ViewModel And Adding To / Accessing List
Apr 12, 2010
I am trying to create a view that would contain a set of fields that tie to an AgencyAddress object, an add button, and under those fields, a list of AgencyAddress that would show addresses that were added. My view works, and the fields are properly bound, however I can't seem to get the list to maintain previously added addresses.
What happens, is that every time the Add button is pressed, the List is re-created (or VS yells at me to create a new instance) and only contains one record. I'm sure I'm missing something simple, but I can't figure out how to pass the list back and forth and be able to add to it. Two weeks on my own is where I draw the line! My ViewModel:
[Code]....
My Create controller action (HTTP Get) is here:
[Code]....
And finally, my HTTP Post Create action:
[Code]....
The loop in the view that should show addresses:
[Code]....
View 3 Replies
Similar Messages:
Sep 3, 2010
I am creating the NerdDinner application via the Professional ASP.NET MVC 2 book, and once I got to the ViewModel part (pages 88-91), I noticed none of my data was saving any longer. I discovered several errors and areas of confusion in these pages of the book, which I will list now.
Page 88: When it says to add the DinnerFormViewModel class, it doesn't say what folder to add it to. On page 89, the Inherits example (NerdDinner.Controllers.DinnerFormViewModel) implies it was added to the Controllers folder, but if that were so, the code on page 88 would need to use Models.Dinner instead of just Dinner. No big deal; just put it in Models and change the Inherit statement on page 89 to use NerdDinner.Models.DinnerFormViewModel.
Page 89: At the bottom of the page is some wrong code. Since the properties of Dinner are now one level deeper, all references to m.Title, m.Country, etc, need to be m.Dinner.Title, m.Dinner.Country, etc. The statements above the code are also incorrect ("Notice... we are not changing the names of the input elements... the form elements will still be named...") -- the form elements will actually change from "Title", "Country", etc to "Dinner_Title", "Dinner_Country", etc.
Page 90 (Code Snippet 1-53): This was the one that broke the data saving. Now that the form elements have prefixes, the TryUpdateModel(dinner) needs to pass a prefix, like TryUpdateModel(dinner, "Dinner"). This will append "Dinner_" to the element names so that they can be found on the form.
View 3 Replies
Feb 28, 2011
I have the following viewmodel definition
public class AccessRequestViewModel
{
public Request Request { get; private set; }
public SelectList Buildings { get; private set; }
public List<Person> Persons { get; private set; }
}
So in my application there must be at least 1 person for an access request. What approach might you use to validate? I don't want this validation to happen in my controller which would be simple to do. Is the only choice a custom validation attribute?
Edit: Currently performing this validation with FluentValidation (nice library!)
RuleFor(vm => vm.Persons)
.Must((vm, person) => person.Count > 0)
.WithMessage("At least one person is required");
View 4 Replies
Feb 6, 2010
I have the following Students class:
[Code]....
I need the 1 since it's a foreign key in another table. For the life of me, I can't get this to work like this.
View 7 Replies
Jan 25, 2011
I'm trying to add Div Objects to an array and trying to access them later when I call my loadViews function. All of my alerts fire, in the proper order, but the array m_Divs is always of length 0.
I'm re-registering the script each time on Page_Load, due to it throwing an "Error: Object expected" after each page_load when trying to call the javascript if I don't.
.JS file.
[Code]....
View 3 Replies
Mar 17, 2011
i have two drop down list in web form..for both of them i've used following code to bind with sql..but whenever i'm trying to bind second drop down list with same method..it's giving error..
the code i've used:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlPropertyType.AppendDataBoundItems = true;
String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
String strQuery = "select ID, PropertyName from PropertyType";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ddlPropertyType.DataSource = cmd.ExecuteReader();
ddlPropertyType.DataTextField = "PropertyName";
ddlPropertyType.DataValueField = "ID";
ddlPropertyType.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
View 2 Replies
Aug 11, 2010
I have two lists:
List<string> _list1;
List<string> _list2;
I need add all _list2 different items on _list1...
How can I do that using LINQ?
View 3 Replies
Jun 29, 2010
I have a dropdown list populated from the database. There is also a button next to the dropdownlist that once clicked on. It takes the item in the dropdownlist and use that to populate the gridview.however though, I would like to add a blank to the dropdownlist and message that would say, you have selected a blank whenever the user selects the blank from the dropdownlist and clicks on the button.
View 7 Replies
May 5, 2010
My code behind reads from a database and depending on the results depends on the display. It then allows the user to choose one item. However, I want to also give a checkboxlist to allow multiple options.
My function shows (currently only one option available to select at a time)
[code]....
View 2 Replies
Mar 10, 2011
I have this food recipe asp.net c# application I'm working on and I would like to be able to insert a list of tags into the database for each recipe. So let's say I have three tags (southern, cajun, gumbo) that correspond to a gumbo recipe. How would I insert these three tags into the database from the UI? Would I store them in a List, run a foreach, and insert each tag individually? I'd really like to be able to somehow use a stored procedure to do this.
View 2 Replies
Aug 30, 2010
I am trying to search a list box by adding a text box next to it.
in the textbox_textchanged I added below code
'Retrieve the item's listindex
Dim i
As Integer = List1.FindString(Text1.Text)
'highligting the matched ones
List1.SelectedIndex = i
But I am getting Below error
Error 1 'FindString' is not a member of 'System.Web.UI.WebControls.ListBox'
View 7 Replies
Nov 8, 2010
in one page i have list box on one button click i am showing on popup which is another aspx pagein that page showing one grid with check box now i want that when user select check boxes and click on add button selected items should gets added to list box on page when i click save button on page(not popup) all the records in that list box should gets saved to data base how to do this
View 4 Replies
Feb 22, 2010
if i create a html ul. and wanted to dynamically add list items to this list.
so i have :
<ul id="test" runat="server">
</ul>
is there a way i can add list item to this list dynamically in asp.net i am using vb
View 1 Replies
Dec 2, 2010
I have written the piece of code below to get a productID from a gridView when the user clicks on the select link.
Convert.ToInt32(GridView1.SelectedDataKey.Values["productID"])))
However, if a user clicks on this more than one click the newer value replaces the previous. Is there a way to keep adding to the cart list when the user clicks on a new item?
Edit:
Here's my code for the Shopping Page:
[Code]....
I dont know if the location of creating the list is important? Wasn't sure if it was placed in the click event if it would keep creating a new instance?
Then for the Basket Page I have:
[Code]....
View 3 Replies
Feb 7, 2011
I have a web page where users need to enter customer contact information. They could enter from 0 to an infinite number of contacts.
I created this page code on page:
<ajaxToolkit:ToolkitScriptManager runat="Server" EnablePartialRendering="true" ID="ScriptManager1" />
<asp:PlaceHolder ID="phCustomerContacts" runat="server" EnableViewState="true">/asp:PlaceHolder>
<asp:LinkButton ID="btnAddContact" runat="server" OnClick="btnAddContact_Click" CssClass="LinkButton" Text="Add Contact"/>
In my code behind I added this:
public void btnAddContact_Click(object sender, EventArgs e)
{
IList<CustomerContactProfile> customerContacts = new List<CustomerContactProfile>();
if (ViewState["CustomerContactList"] != null)
customerContacts = (List<CustomerContactProfile>)ViewState["CustomerContactList"];
CustomerContactProfile contactProfile = (CustomerContactProfile)LoadControl("~/Controls/Embedded/CustomerContactProfile.ascx");
customerContacts.Add(contactProfile);
foreach (CustomerContactProfile contact in customerContacts)
phCustomerContacts.Controls.Add(contact);
ViewState["CustomerContactList"] = customerContacts;
}
This code doesn't work because the ViewState can't handle storing all of that control data. However, I cannot think of another way to store the controls that were already added.
The viewstate of the asp:PlaceHolder control doesn't save anything and I need the controls to be saved so that if a user puts in some data to the first control that the data isn't lost when they add a second one and so on.
View 6 Replies
Nov 5, 2010
In my Page_Load() event, I'm calling a Sub (in a module UserMenu.vb) that reads an XML file which has the user menu entries. The final menu should be an <ul> with several <li>, some of them with anidated <ul> within. As I understand, I may use a asp:Panel in my page to position a container for this elements I read and in this sub I try to add them using new BulletedList and new ListElement BList.Items.Add(LItem)), but when I get to Panel.Controls.Add(BList) it throws an error that says BList should be inserted in a Form. Why? Aren't they just <ul> and <li>? What other alternative I have to create <ul> and <li> in a specific part of my page?
View 3 Replies
Mar 22, 2011
I am getting Range1 and Range2 from database. Dataset is like below.
Range1 Range2
AB100 XY200
AB300 XY400
AB500 XY600
I have to bind these ranges to a Drop Down List as list items includung range number in my application.So list item should be like ==> 1 AB100 XY200 . When user selects a range I have to pass Range1 and Range2 to database.How can I bind the two ranges with range number to Drop Down List.
View 1 Replies
May 11, 2010
I have a table of products on my page, each product has zero or more colors, the colors are shown as a list beneath the product. After the colors I have a button to add a color. The button will do an ajax call with the parent product id to a controller which will return a JSON object with color information. My problem is where to store the product id in the DOM, should I put it in a hidden field and use jquery in the click event of the "add color" to get to it? What is the best way to do this?
EDIT: The page is initially rendered on the server so I don't want to use jquery to add the id's to the page.
View 1 Replies
Jun 22, 2010
adding data from more than one columns of a table in drop down list but not concatening them.
Like a table having columns Station_1 , Station_2 , Station_3. All these columns having place names.
Now I want to list the distinct station/place names from each columns Station_1, Station_2 and Station_3 in my drop down list.
How to do this I serched everywhere in but not found the solution..
View 4 Replies
Jun 4, 2010
I'm looking for a good way to add arbitrary properties to the objects in a strongly typed list, based on the principle that I shouldn't pass a DataTable from my business layer to my presentation layer.For example, I might have a Category class with the properties CategoryId and Title. On one page I would like to fetch a list of all categories (ie. List<Category>) together with the most expensive product in each category.A while ago, I would have just returned a DataTable with some additional columns in it with the product data in, but I'm trying not to do that -- it would be trivial to set up it's not good practice. One option is to add a MostExpensiveProduct property to my Category class, but I might want to display the most recently added product in another case, or the cheapest product, so I'd end up adding a lot of properties to cover all the options. This just doesn't feel right to me.Am I missing a trick here? What is the best way of doing this? Or should I just be returning a DataTable to which I can add as many columns as I need and not worry about it?
View 3 Replies
Feb 18, 2010
I have a webpart which contains a dropdownlist in the Webpaart property and its working fine. The code is below.
public enum ItemCount
{
One = 1,
Five = 5,
Ten = 10
[Code]....
View 2 Replies
Aug 22, 2010
I'm having arraylist to add student info but i have to create unique id per record below is my class.Student
objStudent=new Student(123,"Nj");statArray.Add(objStudent)public class Student{ private string name; private int RollId; public string Name { get { return name; } set { name = value; } } public int Id { get { return RollId; } set { RollId = value; } } public Employee(string name, int id) { { this.name=name; this.Id=id; } }
View 6 Replies
Feb 24, 2010
I inherited an application written for VB.Net and am converting it over to ASP.Net, with VB as the back end. I have a data pull that adds values to a new instance of a class, then adds the item to a listbox. It worked great in the VB.Net version, but I get the following error in the Visual Studio Text Editor when I try to use the code:
Overload resolution failed because no accessible 'Add' can be called with these arguments:
'Public Sub Add(Item As System.Web.UI.WebControls.ListItem)':Value of type 'MyItem' cannot be converted to System.Web.UI.WebControls.ListItem'.
'Public Sub Add(Item as String)':Value of type 'MyItem' cannot be converted to String.
I have played around with the code quite a bit and have been researching online, but I'm just drawing a blank here.
I put the class("MyItem") in the "App_Code" folder in a file called "MyItem.vb". The class code is as follows:
[Code]....
The code I have for the sub that is supposed to add the items to the listbox is as follows:
[Code]....
View 6 Replies
Feb 26, 2010
I have a GridView control that contians, among other things, a DropDownList. In this particular case, the DropDownList is in the EmptyDataTemplate, but I suspect I will run into a similar situation when I'm in Data Rows as well.I'm trying to handle the OnSelectedIndexChanged event for the DropDownList and in the .aspx file I have used the declarative syntax to assign the handler OnSelectedIndexChanged="ddlStoreList_SelectedIndexChanged") and have created the handler code to deal with this.
[Code]....
The problem that I'm running into is that the event is not being handled. In other words, when I put a breakpoint in the handler, I find that it is not getting hit when I change the selection of the DropDownList.
View 2 Replies
Mar 22, 2011
I am getting Range1 and Range2 from database. Dataset is like below.
Range1 Range2
AB100 XY200
AB300 XY400
AB500 XY600
I have to bind these ranges to a Drop Down List as list items includung range number in my application.So list item should be like ==> 1 AB100 XY200 . When user selects a range I have to pass
Range1 and Range2 to database.
How can I bind the two ranges with range number to Drop Down List.
View 5 Replies