根据上一个列表中选择的值填充下拉列表(asp.net mvc3)

发布于 2024-10-24 03:38:50 字数 201 浏览 0 评论 0原文

我正在尝试使用 MVC 3 和 razor 编写强类型的“创建”视图。我希望用户能够从下拉列表中选择客户(我使用控制器中的 ViewBag 从数据库中填充此列表)。当用户选择一个客户时,我想要一个单独的下拉列表来生成属于该客户的狗列表,这些狗与数据库中的 customerID 相关。然后,表单上的创建按钮将获取这两个值以及其他字段并将其保存到数据库中。

非常感谢任何帮助

I'm trying to progam a strongly typed "Create" view using MVC 3 and razor. I want the user to be able to select a customer from a dropdown list (I populated this from my database using ViewBag in the controller). When the user has selected a customer I want a separate dropdown list to generate a list of dogs belonging to that customer related by the customerID in the database. The create button on the form will then take both of these values along with the the other fields and save it to the database.

Any help is greatly appreciated

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

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

发布评论

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

评论(1

奢华的一滴泪 2024-10-31 03:38:50

我将参考Darin的回答并添加您在这里需要什么,只是因为您并不完全需要他提供的东西。然而,jQuery 设置正是您所寻找的。

添加包含客户属性和狗 ID 的视图模型。

public class CustomerDogCreateViewModel
{
    public string customerFirstName { get; set; }
    public string customerLastName { get; set; }
    // ... more properties from your ViewBag here
    public int dogID { get; set; }

    public CustomerDogCreateViewModel() { }
}

您只需要一个返回 JSON 的控制器操作即可进行部分回发并填充“Dogs”下拉列表。

public ActionResult Dogs(int customerID)
{
    // this is pseudo-LINQ, if you need help with your query,
    // we will need to know a little more about your data objects
    var dogs = // get dogs from your db
    select new
    {
        Id = dog.ID,
        Text = "Fido"
    };

    return Json(dogs, JsonRequestBehavior.AllowGet);
}

对于额外的客户数据,请将这些值添加为创建表单中的隐藏输入

<form action="/YourController/Create" method="POST" >
    <!-- select lists -->
    <input type="hidden" name="customerFirstName" value="@ViewBag.customerFirstName" />
    <input type="hidden" name="customerLastName" value="@ViewBag.customerLastName" />
    <input type="submit" value="Create" />
</form>

,并且您可以按照 Darin 的响应进行相同的连接。有一件事值得一提;在我的回答中,只有 Dog 的 ID 属性会发布,因此一旦请求发生在服务器端,您就需要检索该对象并持久保存到您的数据库中,但是您的业务逻辑是有意义的。

[HttpPost]
public ActionResult Create(CustomerDogCreateViewModel vm)
{
    var dog = YourDb.Dogs.Where(d => d.ID == vm.dogID).SingleOrDefault();
    // you didn't identify your data objects, so persist these to your db as usual.
    return View("CreateSuccess");
}

I'll reference Darin's answer and add what you need here, only since you don't need exactly what he provided. However, the jQuery setup is exactly what you're looking for.

Add a View Model that contains the customer properties and dog ID.

public class CustomerDogCreateViewModel
{
    public string customerFirstName { get; set; }
    public string customerLastName { get; set; }
    // ... more properties from your ViewBag here
    public int dogID { get; set; }

    public CustomerDogCreateViewModel() { }
}

You'll only need one Controller action that returns JSON for the partial postback and populating of the "Dogs" dropdown.

public ActionResult Dogs(int customerID)
{
    // this is pseudo-LINQ, if you need help with your query,
    // we will need to know a little more about your data objects
    var dogs = // get dogs from your db
    select new
    {
        Id = dog.ID,
        Text = "Fido"
    };

    return Json(dogs, JsonRequestBehavior.AllowGet);
}

For your extra customer data, add these values as hidden inputs inside your create form

<form action="/YourController/Create" method="POST" >
    <!-- select lists -->
    <input type="hidden" name="customerFirstName" value="@ViewBag.customerFirstName" />
    <input type="hidden" name="customerLastName" value="@ViewBag.customerLastName" />
    <input type="submit" value="Create" />
</form>

And you can identically wire it up per Darin's response. One thing to mention; in my answer, only the Dog's ID attribute will post, so once the request is server-side, you'll need to retrieve that object and persist to your db however your business logic makes sense.

[HttpPost]
public ActionResult Create(CustomerDogCreateViewModel vm)
{
    var dog = YourDb.Dogs.Where(d => d.ID == vm.dogID).SingleOrDefault();
    // you didn't identify your data objects, so persist these to your db as usual.
    return View("CreateSuccess");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文