C# 如何将当前模型发送到UIHint视图
简介: 我有一个网格,其中有一个名为Value的字段,用户可以编辑该字段 在我的模型上,我有一个带有可能值的IEnumerable列表,如果用户试图编辑时可能值=空(网格上的值字段),默认情况下,我应该显示一个文本框,但如果可能值有值,我必须显示一个包含所有可能值的下拉列表 问题: 在编辑模式下使用UIHint,我可以显示dropDownList,但我不知道如何将当前模型发送到Overview 代码: 型号:C# 如何将当前模型发送到UIHint视图,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,简介: 我有一个网格,其中有一个名为Value的字段,用户可以编辑该字段 在我的模型上,我有一个带有可能值的IEnumerable列表,如果用户试图编辑时可能值=空(网格上的值字段),默认情况下,我应该显示一个文本框,但如果可能值有值,我必须显示一个包含所有可能值的下拉列表 问题: 在编辑模式下使用UIHint,我可以显示dropDownList,但我不知道如何将当前模型发送到Overview 代码: 型号: public class FamilyParameter { public F
public class FamilyParameter
{
public FamilyParameter()
{
PossibleValues = new List<string>();
}
[DisplayName("Value")]
[UIHint("_FamilyParameterValue")]
public string Value { get; set; }
public IEnumerable<string> PossibleValues { get; set; }
}
}
视图:主视图
<div style="height:270px" >
<table>
<tr>
<td width="100%">
<br />
@(Html.Telerik().Grid<FamilyParameter>()
.Name("GridParam")
.DataKeys(keys => keys.Add(param => param.Code))
.HtmlAttributes(new { style = "width: 420px;" })
.NoRecordsTemplate("No existen resultados...")
.DataBinding(
dataBinding => dataBinding.Ajax()
.Select("_EditMaterial_SelectParameters", Controllers.Valoration,Model)
.Update("_EditMaterial_UpdateParameters", Controllers.Valoration,Model)
)
.Columns(columns =>
{
columns.Bound(param => param.Code).Width("75px").HtmlAttributes(new { style = "text-align: left;" }).ReadOnly();
columns.Bound(param => param.Description).Width("200px").HtmlAttributes(new { style = "text-align: left;" }).ReadOnly();
columns.Bound(param => param.Value).Width("65px").HtmlAttributes(new { style = "text-align: left;" });
columns.Command(commands =>
commands.Edit().ButtonType(GridButtonType.Image)
).Width(60);
})
.Editable(editing => editing.Mode(GridEditMode.InLine))
.Scrollable(scrolling => scrolling.Height(140))
.Footer(false)
.Sortable()
.Resizable(resizing => resizing.Columns(true))
.Reorderable(reorder => reorder.Columns(true))
)
</td>
</tr>
</table>
</div>
<div style="height:270px" >
<table>
<tr>
<td width="100%">
<br />
@(Html.Telerik().Grid<FamilyParameter>()
.Name("GridParam")
.DataKeys(keys => keys.Add(param => param.Code))
.HtmlAttributes(new { style = "width: 420px;" })
.NoRecordsTemplate("No existen resultados...")
.DataBinding(
dataBinding => dataBinding.Ajax()
.Select("_EditMaterial_SelectParameters", Controllers.Valoration,Model)
.Update("_EditMaterial_UpdateParameters", Controllers.Valoration,Model)
)
.Columns(columns =>
{
columns.Bound(param => param.Code).Width("75px").HtmlAttributes(new { style = "text-align: left;" }).ReadOnly();
columns.Bound(param => param.Description).Width("200px").HtmlAttributes(new { style = "text-align: left;" }).ReadOnly();
columns.Bound(param => param.Value).Width("65px").HtmlAttributes(new { style = "text-align: left;" });
columns.Command(commands =>
commands.Edit().ButtonType(GridButtonType.Image)
).Width(60);
})
.Editable(editing => editing.Mode(GridEditMode.InLine))
.Scrollable(scrolling => scrolling.Height(140))
.Footer(false)
.Sortable()
.ClientEvents(e => e.OnEdit("OnEditGridParam"))
.Resizable(resizing => resizing.Columns(true))
.Reorderable(reorder => reorder.Columns(true))
)
</td>
</tr>
</table>
</div>
<script type="text/javascript">
function OnEditGridParam(e) {
var item = { id: e.dataItem.Code };
$.ajax({
url: "/Valoration/_EditMaterial_EditParameters",
type: "POST",
async: false,
data: item,
success: function (data, status, xhr) {
$.post(data.familyParameterValueView, item, function (partial) { $('#divFamilyParameterValue').html(partial); });
},
error: function (xhr, status, err) {
alert(err);
}
});
}
</script>
@(Html.Telerik().Grid())
.Name(“GridParam”)
.DataKeys(keys=>keys.Add(param=>param.Code))
.HtmlAttributes(新的{style=“width:420px;”})
.NoRecordsTemplate(“不存在结果…”)
.数据绑定(
dataBinding=>dataBinding.Ajax()
.Select(“\u EditMaterial\u SelectParameters”,Controllers.Valoration,Model)
.Update(“\u EditMaterial\u UpdateParameters”,Controllers.Valoration,Model)
)
.列(列=>
{
columns.Bound(param=>param.Code).Width(“75px”).HtmlAttributes(新的{style=“text align:left;”).ReadOnly();
columns.Bound(param=>param.Description).Width(“200px”).HtmlAttributes(新的{style=“text align:left;”).ReadOnly();
columns.Bound(param=>param.Value).Width(“65px”).HtmlAttributes(新的{style=“text align:left;”);
columns.Command(commands=>
commands.Edit().ButtonType(GridButtonType.Image)
).宽度(60);
})
.Editable(editing=>editing.Mode(GridEditMode.InLine))
.可滚动(滚动=>滚动高度(140))
.Footer(假)
.Sortable()
.resizeable(resize=>resize.Columns(true))
.Reorderable(reorder=>reorder.Columns(true))
)
我已经解决了
也许这不是最好的方法,但效果100%很好:
工作原理:
在我的UIHint视图中,我创建了一个简单的Div。每次用户在我的网格上进入编辑模式时,我都会执行一个带有ajax post操作的Javascript,该操作将返回一个url。然后我将执行url以加载PartialView,一个带有DropDownList,另一个带有TextBox
希望有帮助
完整代码:
视图:Shared/EditorTemplates/_FamilyParameterValue.cshtml
@model Bpt.Domain.FamilyParameter
@Html.Telerik().DropDownList().Name("demo");
<div id="divFamilyParameterValue"> </div>
视图:主视图
<div style="height:270px" >
<table>
<tr>
<td width="100%">
<br />
@(Html.Telerik().Grid<FamilyParameter>()
.Name("GridParam")
.DataKeys(keys => keys.Add(param => param.Code))
.HtmlAttributes(new { style = "width: 420px;" })
.NoRecordsTemplate("No existen resultados...")
.DataBinding(
dataBinding => dataBinding.Ajax()
.Select("_EditMaterial_SelectParameters", Controllers.Valoration,Model)
.Update("_EditMaterial_UpdateParameters", Controllers.Valoration,Model)
)
.Columns(columns =>
{
columns.Bound(param => param.Code).Width("75px").HtmlAttributes(new { style = "text-align: left;" }).ReadOnly();
columns.Bound(param => param.Description).Width("200px").HtmlAttributes(new { style = "text-align: left;" }).ReadOnly();
columns.Bound(param => param.Value).Width("65px").HtmlAttributes(new { style = "text-align: left;" });
columns.Command(commands =>
commands.Edit().ButtonType(GridButtonType.Image)
).Width(60);
})
.Editable(editing => editing.Mode(GridEditMode.InLine))
.Scrollable(scrolling => scrolling.Height(140))
.Footer(false)
.Sortable()
.Resizable(resizing => resizing.Columns(true))
.Reorderable(reorder => reorder.Columns(true))
)
</td>
</tr>
</table>
</div>
<div style="height:270px" >
<table>
<tr>
<td width="100%">
<br />
@(Html.Telerik().Grid<FamilyParameter>()
.Name("GridParam")
.DataKeys(keys => keys.Add(param => param.Code))
.HtmlAttributes(new { style = "width: 420px;" })
.NoRecordsTemplate("No existen resultados...")
.DataBinding(
dataBinding => dataBinding.Ajax()
.Select("_EditMaterial_SelectParameters", Controllers.Valoration,Model)
.Update("_EditMaterial_UpdateParameters", Controllers.Valoration,Model)
)
.Columns(columns =>
{
columns.Bound(param => param.Code).Width("75px").HtmlAttributes(new { style = "text-align: left;" }).ReadOnly();
columns.Bound(param => param.Description).Width("200px").HtmlAttributes(new { style = "text-align: left;" }).ReadOnly();
columns.Bound(param => param.Value).Width("65px").HtmlAttributes(new { style = "text-align: left;" });
columns.Command(commands =>
commands.Edit().ButtonType(GridButtonType.Image)
).Width(60);
})
.Editable(editing => editing.Mode(GridEditMode.InLine))
.Scrollable(scrolling => scrolling.Height(140))
.Footer(false)
.Sortable()
.ClientEvents(e => e.OnEdit("OnEditGridParam"))
.Resizable(resizing => resizing.Columns(true))
.Reorderable(reorder => reorder.Columns(true))
)
</td>
</tr>
</table>
</div>
<script type="text/javascript">
function OnEditGridParam(e) {
var item = { id: e.dataItem.Code };
$.ajax({
url: "/Valoration/_EditMaterial_EditParameters",
type: "POST",
async: false,
data: item,
success: function (data, status, xhr) {
$.post(data.familyParameterValueView, item, function (partial) { $('#divFamilyParameterValue').html(partial); });
},
error: function (xhr, status, err) {
alert(err);
}
});
}
</script>
视图:_FamilyParameterValueTextBox.cshtml
@model Bpt.Domain.FamilyParameter
@Html.TextBoxFor(e => e.Value)
我注意到你用的是Telerik,我用的是剑道,语法看起来是一样的,这是我用的网格
@(Html.Kendo().Grid(Model)
.Name("grdDocumentManager")
//Column Binding
.Columns(columns =>
{
columns.Bound(dm => dm.DocumentNote).Title("Note").Width("20px").ClientTemplate("#if(trim(DocumentNote) != \"\"){#" +
"<img src=\"/content/i_discuss.gif\" title=\"View Notes\" onclick=\"javascript:showWindow(#=DocumentId#, '#=CaseId#');\" width=\"16\" height=\"16\" border=\"0\" style=\"cursor:pointer;\">#}" +
"else{#" +
"<img src=\"/content/i_rate.gif\" title=\"Add Note\" onclick=\"javascript:showWindow(#=DocumentId#, '#=CaseId#');\" width=\"16\" height=\"16\" border=\"0\" style=\"cursor:pointer;\">" +
"#}#");
columns.Bound(dm => dm.DocumentId).Hidden(true);
columns.Bound(dm => dm.CaseId).Width("50px");
columns.Bound(dm => dm.PresidingJudge).ClientGroupHeaderTemplate("Presiding Judge: #=value#").Width("20px");
columns.Bound(dm => dm.MagistrateJudge).ClientGroupHeaderTemplate("Magistrate Judge: #=value#").Width("20px");
columns.Bound(dm => dm.CaseType).Width("20px");
columns.Bound(dm => dm.StatusValue).Width("20px").ClientTemplate("#=Status#").EditorTemplateName("StatusEditor").Title("Status");
columns.Bound(dm => dm.UserName).Width("20px").EditorTemplateName("UserNameEditor");
columns.Bound(dm => dm.CreationDate).Width("50px").Format("{0:g}");
columns.Bound(dm => dm.PageCount).Width("20px");
columns.Command(command => command.Edit()).Width(200);
}
)
.Editable(e => e.Mode(GridEditMode.InLine))
.DataSource(ds => ds.Ajax()
.Model(m => {
m.Id(dm => dm.DocumentId);
m.Field(dm => dm.CaseId).Editable(false);
m.Field(dm => dm.CaseType).Editable(false);
m.Field(dm => dm.CreationDate).Editable(false);
m.Field(dm => dm.DocumentNote).Editable(false);
m.Field(dm => dm.MagistrateJudge).Editable(false);
m.Field(dm => dm.PresidingJudge).Editable(false);
m.Field(dm => dm.PageCount).Editable(false);
})
.Update(update => update.Action("DocumentUpdate","Admin"))
)
)
@(Html.Kendo().Grid(模型)
.名称(“grdDocumentManager”)
//列绑定
.列(列=>
{
columns.Bound(dm=>dm.DocumentNote).Title(“Note”).Width(“20px”).ClientTemplate(“如果(修剪(DocumentNote)!=\”\””){”+
"#}" +
“否则{#”+
"" +
"#}#");
columns.Bound(dm=>dm.DocumentId).Hidden(true);
columns.Bound(dm=>dm.CaseId).Width(“50px”);
columns.Bound(dm=>dm.PresidingJudge).ClientGroupHeaderTemplate(“主审法官:#=value#”).Width(“20px”);
columns.Bound(dm=>dm.magitalJudge).ClientGroupHeaderTemplate(“治安法官:#=value#”).Width(“20px”);
columns.Bound(dm=>dm.CaseType).Width(“20px”);
columns.Bound(dm=>dm.StatusValue).Width(“20px”).ClientTemplate(“状态”).EditorTemplateName(“状态编辑器”).Title(“状态”);
columns.Bound(dm=>dm.UserName).Width(“20px”).EditorTemplateName(“UserNameEditor”);
columns.Bound(dm=>dm.CreationDate).Width(“50px”).Format(“{0:g}”);
columns.Bound(dm=>dm.PageCount).Width(“20px”);
columns.Command(Command=>Command.Edit()).Width(200);
}
)
.Editable(e=>e.Mode(GridEditMode.InLine))
.DataSource(ds=>ds.Ajax()
.Model(m=>{
m、 Id(dm=>dm.DocumentId);
m、 字段(dm=>dm.CaseId)。可编辑(false);
m、 字段(dm=>dm.CaseType)。可编辑(false);
m、 字段(dm=>dm.CreationDate)。可编辑(false);
m、 字段(dm=>dm.DocumentNote).可编辑(false);
m、 字段(dm=>dm.madgetridger)。可编辑(false);
m、 字段(dm=>dm.PresidingJudge).可编辑(false);
m、 字段(dm=>dm.PageCount)。可编辑(false);
})
.Update(Update=>Update.Action(“文档更新”、“管理”))
)
)
如果您注意到列绑定中有一个名为EditorTemplate的属性,那么您可以在其中定义编辑器的名称(确保在编辑器中控件的名称与绑定的属性的名称相同),网格将为其生成正确的视图