在MVC3使用相关ID的形式填充选择框 [英] Populating a select box in a form using related ID in MVC3

查看:153
本文介绍了在MVC3使用相关ID的形式填充选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个机型一个非常简单的数据结构。第一个包含用户名,UserQuestion和userLocationID和另一个与LOCATIONNAME和LocationID,在第一表​​的locationID是关系到LOCATIONNAME的第二表。不过,我还没有指定任何关系。我已经设置了数据结构使用code第一种方法中所使用的>。

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 .

我想创建一个有两个文本输入,用于用户输入他们的姓名和问题,并填充了从第二个表中的所有locationNames一个选择框的形式。不过,我似乎无法创建,让我这样做模型。我需要做一个独立的视图模型?

I would like to create a form which has two text inputs for a user to enter their name and question and 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?

我很新的MVC和点网框架。 。我也看了一下<一个href=\"http://stackoverflow.com/questions/4772318/asp-net-mvc-adding-records-to-related-multiple-tables-from-single-view\">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.

推荐答案

我可以用一个控制器举一个例子,一个视图和三个C#类。要使用此code,创建在Visual Studio中的空MVC2项目,并添加到实体框架DLL版本4.1的引用。如果您需要帮助的地方把这些文件我推荐的史蒂夫·桑德森的MVC2书

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

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();
    }
}

控制器\\ HomeContoller.cs:

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();
    }
}

视图\\首页\\ Index.aspx的

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>

这篇关于在MVC3使用相关ID的形式填充选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆