在 MVC3 中使用相关 ID 填充表单中的选择框

发布于 2024-10-15 01:53:30 字数 646 浏览 7 评论 0原文

我有一个非常简单的数据结构,有两个模型。第一个包含 UserName、UserQuestion 和 userLocationID,另一个包含 LocationName 和 LocationID,第一个表中的 locationID 与第二个表中的 LocationName 相关。但是我没有指定任何关系。我已经使用 这里

我想创建一个表单,其中有两个文本输入供用户输入其姓名和问题,以及一个填充了第二个表中所有 locationNames 的选择框。然而,我似乎无法创建允许我这样做的模型。我需要制作一个单独的 ViewModel 吗?

有谁知道一个简单的教程可以解释如何做到这一点?

我对 MVC 和 .net 框架还很陌生。 。我查看了 这个答案但我似乎无法修改它以满足我的需要。所以,如果我要求一些非常基本的东西,我很抱歉。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2024-10-22 01:53:30

我可以在一个控制器、一个视图和三个 C# 类中给出一个示例。要使用此代码,请在 Visual Studio 中创建一个空的 MVC2 项目,并添加对 Entity Framework dll 版本 4.1 的引用。如果您需要有关将这些文件放在哪里的帮助,我建议 Steve Sanderson 的 MVC2 书

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:

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文