我有一个应用程序,可以在tabcontrol中同时打开许多视图。
我在所有视图中都在使用淘汰赛。
我有下面的两个动作:
public ActionResult Edit()
{
ViewData["Mode"] = "Edit";
return PartialView("AddOrEdit", new ParametroModel() { Codigo = "banco", Descricao = "String de conexao do banco de dados" });
}
public ActionResult Add()
{
ViewData["Mode"] = "Add";
return PartialView("AddOrEdit", new ParametroModel() { Codigo = "banco", Descricao = "String de conexao do banco de dados" });
}
两者都共享相同的部分视图" addoredIt",请遵循我的视图:
@model CCL.Apoio.Web.Models.ParametroModel
@{
var isEdit = ViewData["Mode"] == "Edit";
var isAdd = ViewData["Mode"] == "Add";
}
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "formParametro" }))
{
@Html.Partial("_FormValidation")
<fieldset>
<legend>Dados do parâmetro</legend>
<dl>
<dt>
<label>Código</label></dt>
@if (isAdd)
{
<dd>@Html.TextBoxFor(m => m.Codigo)</dd>
}
else
{
<dd>@Html.DisplayFor(m => m.Codigo)</dd>
}
</dl>
<dl>
<dt>
<label>Descrição</label></dt>
<dd>
@Html.TextAreaFor(m => m.Descricao)</dd>
</dl>
<dl>
<dt>
<label>Valor</label></dt>
<dd>
@Html.TextAreaFor(m => m.Valor)
</dd>
</dl>
<dl>
<dt> </dt>
<dd>
<input type="submit" value="Cadastrar" onclick="" /></dd>
</dl>
</fieldset>
}
<script type="text/javascript">
@if (isEdit)
{
<text>
var parametroEditModel = ko.mapping.fromJSON('@Html.Raw(Json.Encode(Model))');
parametroEditModel.Save = function () {
ModuloHelper.ajaxPut('/Api/Parametros', parametroEditModel); //Post data
};
ko.applyBindings(parametroEditModel, document.getElementById("formParametro"));
</text>
}
else if (isAdd)
{
<text>
var parametroAddModel = ko.mapping.fromJSON('@Html.Raw(Json.Encode(Model))');
parametroAddModel.Save = function () {
ModuloHelper.ajaxPost('/Api/Parametros', parametroAddModel);
};
ko.applyBindings(parametroAddModel, document.getElementById("formParametro"));
</text>
}
</script>
在我的视图中,我有两个可观察的视图模型,一个用于"添加"模式,另一种用于"编辑"模式。我不能将相同的ViewModel用于两种模式,因为两个操作都打开时,在客户端将具有两个具有相同名称的对象。我是这样做的吗?
我想知道这是否是一种更好的方法来实现这一目标。任何身体是否可以建议另一种更好的方法来做我想要的事情?
tks。
我不确定我完全了解问题,但从我收集的内容中,同一部分视图可能在选项卡控件中的同一页面上存在两次:一个用于添加,一个用于编辑。但是,您只希望它们能够在添加上输入Codigo值。编辑是否应该使用现有的参数模块?
首先,我建议您使您的表单ID与众不同(也许是通过随机添加后修置)。这样,当您将绑定应用于表单时,您始终可以确保对正确的绑定。其次,除了变量名称外,添加和编辑之间的JavaScript似乎没有区别。您最好将其凝结成一个。我注意到的最后一件事是,您的表单元素上没有敲除数据结合属性。你有工作吗?除此之外,只要您的视图中的其他任何地方都没有其他任何地方,您就可以执行该脚本后进行毯子ko.applyBindings(someViewModel)
。