简体   繁体   中英

asp mvc5 adding a table to existing project for dropdown list data

Using an asp MVC5 project, has a working SQL table. Question is how can a 2nd table be added in such that it holds commonly used data such as name, phone, email etc; Goal is to have dropdowns on the main project on a View page so that they will populate or "auto fill" the related fields for name, email etc.

 [Table("newInfoTable")]
public class InfoTable
{

    [Key]
    public int ID { get; set; }
    [Display(Name = "First Name")]
    [StringLength(255)]
    public string firstName { get; set; }
    [Display(Name = "Last Name")]
    [StringLength(255)]
    public string lastName { get; set; }
    [Display(Name = "Contact Phone Number")]
    public string phoneNumber { get; set; }
    [Display(Name = "Email Address")]
    [StringLength(255)]
    public string emailAddress { get; set; }
    }

In the main table, there is a reference to this table:

    public virtual ICollection<InfoTable> InfoTableDat { get; set; }

And all of the variable names in this new info table match names in the Main project table. The new SQL table was created with SQL Management Studio, tested, made a new controller/view and am able to add items/edit or delete to this table from the project.

Within the View, an Edit page, intellisense can see the @Model.InfoTableDat But how to fill the dropdown list?

And is it correct to use "public virtual ICollectioninstanceName" or does that simply link the 2 tables based on the ID? It gets an error:

Server Error in '/' Application.

Invalid column name 'MainProj_ID'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

The main project class is named MainProj.cs in the Model folder, but there's no MainProj_ID, I never use the underscore for any names.

If i understand your question correct, this is how i would do it. You need a dropdown of all your InfoTableDat rows, where the text of each option is the full name from the infotable and the value is the ID

Thefore you have to add the following field to your InfoTable model:

[NotMapped]
public string FullName {
    get {
        return firstName + " " + LastName
    }
}

This makes sure that you have a full name you can use as the text. To get the data from the dropdownlist you have to add the following to your action in your controller:

ViewBag.InfoTables = new SelectList(Model.InfoTableDat, "FullName", "ID");

The "FullName" is the field for the text and "ID" is the field for the value of the dropdown

To display the dropdown the following HtmlHelper is used:

@Html.EditorFor(Model.InfoTableDatID, (SelectList)ViewBag.InfoTables)

To make all of this work, you need a eventlistener in jQuery for when the dropdown changes. This event then needs to do an AJAX call to a method that gets the detail for the specific InfoTable with the ID of the selected option in the dropdown. When youve fetched the data via. AJAX, all there's left is to append the data to the existing fields in your view.

That's how i would do it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM