[英]MVC change html helper type
I have a view for viewing and editing accounts. 我有一个用于查看和编辑帐户的视图。 This has an "edit" button and a "save" button. 这有一个“编辑”按钮和一个“保存”按钮。
I would like the view to display the information as labels and change this to textboxes when edit is clicked. 我希望视图将信息显示为标签,并在单击编辑时将其更改为文本框。 How can I change the lables to textboxes and vice versa? 如何将标签更改为文本框,反之亦然? I currently have a javascript function to hide/show the save button when edit is clicked, presumably I can achieve the result in javascript also? 我目前有一个javascript函数,可在单击编辑时隐藏/显示“保存”按钮,想必我也可以在javascript中实现结果吗?
My view html is below (currently using textbox not label) 我的视图html在下面(当前使用文本框而不是标签)
<script type="text/javascript">
function SetEditButton()
{
var editOrSave = document.getElementById("editButton").value;
if (editOrSave == "Edit") {
//AccountPopupControl.SetHeaderText(AccountPopupControl.HeaderText + " EDIT");
document.getElementById("saveButton").style = "float: right;display:block;"
document.getElementById("editButton").style = "float: right;display:none;"
}
else {
//AccountPopupControl.SetHeaderText(AccountPopupControl.HeaderText.substring(0, AccountPopupControl.HeaderText.length - 5));
document.getElementById("editButton").style = "float: right;display:block;"
document.getElementById("saveButton").style = "float: right;display:none;"
}
return editOrSave;
}
</script>
<table style="width:100%;height:100%">
<tr>
<td>
<div class="verticalLine">
<table style="width:100%;height:100%">
<tr>
<td valign="top">
<p class="big">Name:</p><br />
<p class="big">Reference:</p><br />
<p class="big">Description:</p><br />
<p class="big">Adress Line 1:</p><br />
<p class="big">Adress Line 2:</p><br />
<p class="big">Adress Line 3:</p><br />
<p class="big">Adress Line 4:</p><br />
<p class="big">Adress Line 5:</p><br />
</td>
<td valign="top">
<p class="big">@Html.TextBoxFor(model => model.name, new { id = "nameTxtBx", @readonly = "true" })</p><br />
<p class="big">@Html.TextBoxFor(model => model.reference, new { id = "referenceTxtBx", @readonly = "true" })</p><br />
<p class="big">@Html.TextBoxFor(model => model.description, new { id = "descriptionTxtBx", @readonly = "true" })</p><br />
<p class="big">@Html.TextBoxFor(model => model.address1, new { id = "address1TxtBx", @readonly = "true" })</p><br />
<p class="big">@Html.TextBoxFor(model => model.address2, new { id = "address2TxtBx", @readonly = "true" })</p><br />
<p class="big">@Html.TextBoxFor(model => model.address3, new { id = "address3TxtBx", @readonly = "true" })</p><br />
<p class="big">@Html.TextBoxFor(model => model.address4, new { id = "address4TxtBx", @readonly = "true" })</p><br />
<p class="big">@Html.TextBoxFor(model => model.address5, new { id = "address5TxtBx", @readonly = "true" })</p><br />
</td>
</tr>
</table>
</div>
</td>
<td>
<div>
<table style="width:100%;height:100%">
<tr>
<td valign="top">
<p class="big">Custom 1:</p><br />
<p class="big">Custom 2:</p><br />
</td>
<td valign="top">
<p class="big">@Html.TextBoxFor(model => model.custom1)</p><br />
<p class="big">@Html.TextBoxFor(model => model.custom2)</p><br />
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
What about always using @Html.TextBoxFor
, giving them all a @class
so you can target them with JS and then giving them the html attribute @readonly = true
when you you're viewing it and then you can use some jQuery like $('.TheClassYouMade').attr('readonly', false);
总是使用@Html.TextBoxFor
,给它们一个全都是@class
这样您就可以使用JS定位它们,然后在查看它们时给它们html属性@readonly = true
,然后可以使用$('.TheClassYouMade').attr('readonly', false);
to change them at the same time you change the button. 在更改按钮的同时更改它们。
I think it is worth saying that the best practice, generally, for Create and Edit views are different. 我认为值得一提的是,通常,创建和编辑视图的最佳做法是不同的。 See this post for a discussion on why Create and Edit should not be in the same view. 有关为何不应在同一视图中创建和编辑内容的讨论,请参见本文 。
You could you this kind of logic hide and show the textbox on click. 您可以通过这种逻辑隐藏并在单击时显示文本框。
html html
<span id="my-label">Custom</span>
@Html.TextBoxFor(m => m.Custom, new { id="custom-tb", style="display: none" })
js js
$('#my-label').on('click', function () {
$(this).hide();
var $tb = $('#custom-tb');
$tb.show();
$tb.focus();
});
$('#custom-tb').focusout(function () {
$(this).hide();
$('#my-label').show();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.