【发布时间】:2010-09-30 03:38:40
【问题描述】:
您好,我正在构建我的第一个 MVC 2 应用程序,我对我正在阅读的所有示例感到非常困惑。我还在为他们的 Grid 和其他此类控件使用 Telerik MVC 套件。
我阅读了许多教程并观看了视频,但我不能完全掌握放置 javascript 的模式以及如何将代码块包装在适当的标签中。例如,我正在尝试通过状态(AZ、CA 等)的 DropDownList 将客户端与状态关联起来:
我的 ClientController 如下所示:
void PopulateStates()
{
var tempRepo = RepositoryFactory.CreateRepostiory<State>(RepositoryType.Business);
var states = tempRepo.QueryAll().ToList();
var svmList = states.Select(s => Mapper.Map<State, StateViewModel>(s));
ViewData["States"] = svmList;
}
public ActionResult Index()
{
PopulateStates();
return View();
}
Index.aspx 看起来像这样:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderPartial("ClientGrid"); %>
</asp:Content>
ClientGrid.ascx 如下所示:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%= Html.Telerik().Grid<ViewModels.ClientViewModel>()
.Name("ClientGrid")
.EnableCustomBinding(true)
.DataKeys(keys =>
{
keys.Add(c => c.Id);
})
.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text))
.DataBinding(dataBinding =>
{
dataBinding.Ajax()
.Select("_Select", "Clients", new Telerik.Web.Mvc.GridState())
.Insert("_Insert", "Clients", new Telerik.Web.Mvc.GridState())
.Update("_Save", "Clients", new Telerik.Web.Mvc.GridState())
.Delete("_Delete", "Clients", new Telerik.Web.Mvc.GridState());
})
.Columns(columns =>
{
columns.Bound(c => c.Id);
...
还有 ClientViewModel.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ViewModels.ClientViewModel>" %>
<table>
<tr>
<td>
<div class="editor-label">
<%: Html.LabelFor(model => model.StateName) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.State) %>
<%: Html.ValidationMessageFor(model => model.State) %>
</div>
</td>
</tr>
</table>
最后是编辑器模板/Shared/EditorTemplates/StateViewModel.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ViewModels.StateViewModel>" %>
<%=
Html.Telerik().DropDownList()
.Name("DropDownList")
.BindTo
(
((IEnumerable<ViewModels.StateViewModel>)ViewData["States"])
.Where(s => s.StateType == "State")
.Select(s => new SelectListItem
{
Text = s.FormattedName,
Value = s.Id.ToString(),
Selected = (Model != null) ? (Model.Id == s.Id) : false
})
)
%>
一般来说,我不明白哪个部分最终在客户端以及哪个部分最终在服务器上编译。我也对 lambdas 和/或注册事件的不同表示法感到非常困惑。
lambda 的东西让我很生气。我花了很多时间试图弄清楚何时使用 new {} 和 () => ....
- 这 3 个文件中的 javascript 放在哪里,我需要特殊标签吗?
- 与
<%=和<%:相比,我在哪里使用<%标签? - 为什么有时在
<%中要求以分号结束,例如MyFunction();,而其他时候只是MyFunction()?
谢谢!
【问题讨论】:
标签: javascript jquery asp.net-mvc-2 asp.net-ajax