开发者

Populating a select box in a form using related ID in MVC3

开发者 https://www.devze.com 2023-02-07 22:48 出处:网络
I have a very simple data structure with two models. The first containing UserName, UserQuestion and userLocationID and another with LocationName and LocationID, the locationID in the first table is r

I have a very simple data structure with two models. The first containing UserName, UserQuestion and userLocationID and another with LocationName and LocationID, the locationID in the first table is related to the LocationName the second table. However I've not specified any relationship. I've set up the data structure using the code first method in used here .

I would like to create a form which has two text inputs for a user to enter their name and question and开发者_JS百科 a select box that is populated with all the locationNames from the second table. However I can't seem to create the model that allows me to do so. Do I need to make a separate ViewModel?

Does anyone know of a simple tutorial that will explain how to do this?

I'm quite new at MVC, and the dot net framework. . And I've had a look at this answer but I can't seem to modify it to fit my needs. So Apologies if I'm asking for something really basic.


I can give an example in one controller, one view and three C# classes. To use this code, create an empty MVC2 project in visual studio and add a reference to Entity Framework dll version 4.1. If you need help as to where to put these files I recommend Steve Sanderson's MVC2 book.

public class User
{
    public int ID { get; set; }
    public string UserName { get; set; }
    public string Question { get; set; }

    public virtual Location Category { get; set; }
}

public class Location
{
    public int ID { get; set; }
    public string LocationName { get; set; }
}

Repository

using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;

public class Repository : System.Data.Entity.DbContext
{
    public DbSet<User> User { get; set; }
    public DbSet<Location> Locations { get; set; }

    public Repository()
    {
        this.Database.Connection.ConnectionString = 
            @"Server=.;Database=Test;Integrated Security=SSPI";

        if (!this.Database.Exists())
        {
            this.Database.Create();
            this.Locations.Add(new Location { LocationName = "Queensway" });
            this.Locations.Add(new Location { LocationName = "Shepherds Bush" }); 
            this.SaveChanges();
        }
    }

    public IEnumerable<Location> GetLocations()
    {
        return this.Locations.Where(x => x.ID > -1);
    }

    public Location GetLocation(int id)
    {
        return this.Locations.First(x => x.ID == id);
    }

    public void SaveUser(User user)
    {
        this.User.Add(user);
        this.SaveChanges();
    }
}

Controllers\HomeContoller.cs:

using System.Web.Mvc;

public class HomeController : Controller
{
    Repository repo = new Repository();

    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(User user, int categoryId)
    {
        user.Category = repo.GetLocation(categoryId);
        repo.SaveUser(user);
        return View();
    }
}

Views\Home\Index.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<User>" %>

<html> 
<body>
    <% using (Html.BeginForm())
       {%>
    Username: <%: Html.TextBoxFor(model => model.UserName) %><br />
    Question: <%: Html.TextBoxFor(model => model.Question) %><br />
    Location: <select name="categoryId">
        <% foreach (var location in new Repository().GetLocations())
           {%>
        <option value="<%= location.ID %>">
            <%= location.LocationName %></option>
        <%} %>
    <br />
    </select>
    <p>
        <input type="submit" value="Create" />
    </p>
    <% } %>
</body>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号