如何将 DropDownList 绑定到 Ajax 表单并同时提供发布和获取支持?

发布于 2024-12-24 16:31:14 字数 1986 浏览 1 评论 0原文

我有一个用于注册个人属性的注册表。我使用包含的 Ajax.BeginForm 函数和 JSON 来序列化表单。此表单正在更新属性并显示属性(如果在数据库中可用)。因此,如果用户输入值并单击保存按钮,它就会序列化表单并将其保存到数据库中。当用户调用该页面时;如果有可用记录(如果不只是像往常一样显示空白表单),它还会使用 JSON 来填充数据库中的表单。

请找到下面视图的一部分。

@using (Ajax.BeginForm(
    "savepeople", 
    "people", 
    FormMethod.Post, 
    new AjaxOptions { OnComplete = "savePicture" }, 
    new { id = "frPeople" }))
{
    @Html.ValidationSummary(true, "Problem during saving...")
      <fr>
    <h2>
        Personal Details</h2>
    <br />
        <legend>Person</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.People.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.People.Title)
            @Html.ValidationMessageFor(model => model.People.Title)
        </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.People.Description)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.People.Description)
        @Html.ValidationMessageFor(model => model.People.Description)
    </div>

        <p>
            <input id="saveMarker" type="submit" value="Save" />
        </p>
}

所以我使用这样的模型:

namespace MyProject.Models
{
    public class Profile
    {
        public People People { get; set; }
// I am also referencing other models here...
    }

    public partial class People
    {
        public int PeopleId { get; set; }
        public Guid UserId { get; set; }

        public string Title { get; set; }
        public string Description { get; set; }
// It has other attributes also...
}

控制器具有与 JSON 兼容的经典 post 和 get 函数来调用和更新页面。

我的问题是:我已经能够使用 TextBox 处理这些操作,但我想添加一个预定义的 DropDownList 以将其用于保存/更新和如果数据库中有保存的记录,还会显示值。

您知道如何处理此问题吗?

I have a registration form for registering personal attributes. I am using both included Ajax.BeginForm function and also JSON to serialize the form. This form is updating the attributes and also showing the attributes if available in DB. So if a user enter the values and click the save button it is serializing the form and saving it into the db. When a user call the page; it is also using JSON to fill the form from DB if there is a available record if not just showing a blank form as usual.

Please find a part of the View below.

@using (Ajax.BeginForm(
    "savepeople", 
    "people", 
    FormMethod.Post, 
    new AjaxOptions { OnComplete = "savePicture" }, 
    new { id = "frPeople" }))
{
    @Html.ValidationSummary(true, "Problem during saving...")
      <fr>
    <h2>
        Personal Details</h2>
    <br />
        <legend>Person</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.People.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.People.Title)
            @Html.ValidationMessageFor(model => model.People.Title)
        </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.People.Description)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.People.Description)
        @Html.ValidationMessageFor(model => model.People.Description)
    </div>

        <p>
            <input id="saveMarker" type="submit" value="Save" />
        </p>
}

So I am using a model like:

namespace MyProject.Models
{
    public class Profile
    {
        public People People { get; set; }
// I am also referencing other models here...
    }

    public partial class People
    {
        public int PeopleId { get; set; }
        public Guid UserId { get; set; }

        public string Title { get; set; }
        public string Description { get; set; }
// It has other attributes also...
}

And controller has the classic post and get functions compatible with JSON to call and update the page.

My problem is: I am already able to handle these operations with TextBox's but I want to add a predefined DropDownList to use it for both saving/updating and also showing the values if it has a saved record in db.

Do you have any idea about how tho handle this issue?

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

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

发布评论

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

评论(1

葬シ愛 2024-12-31 16:31:14

这个答案很好地解释了如何使用 DropDownLists :
为 MVC3 创建下拉列表使用实体框架(.edmx 模型)和剃刀视图 &&将数据库记录插入多个表

如果您需要 DropDownList 的字典数据,您可以创建一个 ViewModel,其中可以包含您的 PeopleClass 和下拉列表的字典集合,例如:

public class EmployeeAddViewModel
{
    public People people { get; set; }
    public Dictionary<int, string> dropdownValues { get; set; }
}

或者您可以将该集合分配给 ViewBag多变的。尝试阅读上面链接的问答,如果您偶然发现特定问题,请随时询问:)

This answer explains pretty good on how to use DropDownLists :
Create a Dropdown List for MVC3 using Entity Framework (.edmx Model) & Razor Views && Insert A Database Record to Multiple Tables

If you need kind of dictionary data for the DropDownList you can either create a ViewModel that can contain your PeopleClass and the dictionary collection for the dropdown like :

public class EmployeeAddViewModel
{
    public People people { get; set; }
    public Dictionary<int, string> dropdownValues { get; set; }
}

or you can assign the collection to a ViewBag variable. Try reading the Q&A linked above and if you will stumble upon a particular issue, feel free to ask :)

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