[英]How to bind INamingContainer control in ASP.NET MVC4?
I am creating custom HtmlHelper
that generates some web controls on my page like Panel, Label, TextBox and DropDownList
. 我正在创建自定义的HtmlHelper
,它会在我的页面上生成一些Web控件,例如Panel, Label, TextBox and DropDownList
。 My Panel is INamingContainer
so I can group up the elements on the page. 我的面板是INamingContainer
因此我可以将页面上的元素分组。
Here is a code sample: 这是一个代码示例:
class MyPanel : Panel, INamingContainer
{
}
public static MvcHtmlString Panel(this HtmlHelper html)
{
Panel pnl = new MyPanel();
pnl.ID = "mainPanel";
pnl.BorderStyle = BorderStyle.Solid;
pnl.BorderWidth = Unit.Pixel(1);
pnl.BorderColor = System.Drawing.Color.Black;
Label lblTitle = new Label();
lblTitle.Text = "Title";
TextBox txtTitle = new TextBox();
txtTitle.ID = "txtTitle";
lblTitle.Attributes.Add("for", "txtTitle");
Label lblMessage = new Label();
lblMessage.Text = "Message";
TextBox txtMessage = new TextBox();
txtMessage.TextMode = TextBoxMode.MultiLine;
txtMessage.ID = "txtMessage";
Label lblDepartment = new Label();
lblDepartment.Text = "Department";
DropDownList lstDepartment = new DropDownList();
lstDepartment.ID = "lstDepartment";
ListItemCollection collection = new ListItemCollection();
collection.Add(new ListItem("Department1"));
collection.Add(new ListItem("Department3"));
collection.Add(new ListItem("NoDepartment"));
lstDepartment.DataSource = collection;
lstDepartment.DataBind();
pnl.Controls.Add(lblTitle);
pnl.Controls.Add(txtTitle);
pnl.Controls.Add(lblMessage);
pnl.Controls.Add(txtMessage);
pnl.Controls.Add(lblDepartment);
pnl.Controls.Add(lstDepartment);
HtmlTextWriter writer = new HtmlTextWriter(new StringWriter());
pnl.RenderControl(writer);
return MvcHtmlString.Create(writer.InnerWriter.ToString());
}
Now, I need to bind this elements in my controller. 现在,我需要在控制器中绑定此元素。 What I get (as HTML code) when I execute the application (from the above code) is this: 当我从上面的代码执行应用程序时,得到的内容(作为HTML代码)是这样的:
<div id="mainPanel" style="border-color:Black;border-width:1px;border-style:Solid;">
<span for="txtTitle">Title</span>
<input name="mainPanel$txtTitle" type="text" id="mainPanel_txtTitle">
<span>Message</span>
<textarea name="mainPanel$txtMessage" rows="2" cols="20" id="mainPanel_txtMessage"></textarea>
<span>Department</span>
<select name="mainPanel$lstDepartment" id="mainPanel_lstDepartment">
<option value="Department1">Department1</option>
<option value="Department3">Department3</option>
<option value="NoDepartment">NoDepartment</option>
</select>
</div>
What arguments I should expect, and how to bind them? 我应该期待什么参数,以及如何约束它们?
EDIT: 编辑:
I have created some partial Razor view's in the Editor Templates
folder. 我在“ Editor Templates
文件夹中创建了部分Razor视图。
_TicketTitle.cshtml _TicketTitle.cshtml
@model TicketSystemMVC.Models.Ticket
@Html.EditorFor(m => m.Title)
After that in my model , I did this: 之后,在我的模型中 ,我这样做了:
namespace TicketSystemMVC.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public partial class Ticket
{
public Ticket()
{
this.Ticket_Message = new HashSet<Ticket_Message>();
}
public int TicketID { get; set; }
[Required]
[StringLength(50, MinimumLength = 5)]
[UIHint("_TicketTitle")]
public string Title { get; set; }
[Required]
[StringLength(400, MinimumLength = 5)]
[UIHint("_TicketMessage")]
public string Message { get; set; }
[DisplayName("Active ticket?")]
public bool IsActive { get; set; }
[DisplayName("Created on")]
public System.DateTime CreatedOn { get; set; }
public int AccountID { get; set; }
[Required]
[UIHint("_TicketDepartment")]
public int DepartmentID { get; set; }
public virtual Account Account { get; set; }
public virtual Department Department { get; set; }
public virtual ICollection<Ticket_Message> Ticket_Message { get; set; }
}
}
What I do in the controller is this (just simple code to debug and check the values) 我在控制器中所做的就是(只需简单的代码即可调试和检查值)
[HttpPost]
public ActionResult Create(Ticket ticket)
{
string title = ticket.Title;
string message = ticket.Message;
string dep = ticket.DepartmentID.ToString();
return View();
}
The view : 视图 :
@model TicketSystemMVC.Models.Ticket
@using TicketSystemMVC
@{
ViewBag.Title = "Create new ticket";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Ticket</legend>
@Html.Panel()
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Go back", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
I THINK (if I am wrong, please tell me and I delete this answer) what you want to achive can be done easily via editor templates : 我想你想才达到可以通过轻松完成什么(如果我错了,请告诉我,我删除这个答案) 编辑模板 :
ListItemPanel
in Views/Shared/EditorTemplates
在Views/Shared/EditorTemplates
创建一个名为ListItemPanel
的局部视图 @model string
(or whatever fits your type) 定义其模型,例如@model string
(或任何适合您的类型) "fill the template" like: “填充模板”,例如:
<div id="mainPanel" style="border-color:Black;border-width:1px;border-style:Solid;"> <span for="txtTitle">@Html.LabelFor(m => m)</span> <input name="mainPanel$txtTitle" type="text" id="mainPanel_txtTitle"> <span>Message</span> <textarea name="mainPanel$txtMessage" rows="2" cols="20" id="mainPanel_txtMessage"></textarea> <span>Department</span> @Html.DropDownFor(m => m) </div>
In your view you then can do something like: 在您看来,您可以执行以下操作:
@Html.EditorFor(m => m.Department, "ListItemPanel")
In your [HttpPost] public ActionResult Create(MODEL model)
you then get the correctly mapped values in eg model.Department
. 然后在[HttpPost] public ActionResult Create(MODEL model)
,在model.Department
获取正确映射的值。
ATTENTION: This is just a basic "way to go" example and not "production ready copy & paste code"! 注意:这只是一个基本的“前进方式”示例,而不是“生产就绪的复制和粘贴代码”!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.