如何将 DropDownList 绑定到 Ajax 表单并同时提供发布和获取支持?
我有一个用于注册个人属性的注册表。我使用包含的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个答案很好地解释了如何使用 DropDownLists :
为 MVC3 创建下拉列表使用实体框架(.edmx 模型)和剃刀视图 &&将数据库记录插入多个表
如果您需要 DropDownList 的字典数据,您可以创建一个 ViewModel,其中可以包含您的 PeopleClass 和下拉列表的字典集合,例如:
或者您可以将该集合分配给 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 :
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 :)