[英]How to overwrite one model property in ASP.Net core
I have a model with say 10 properties.我有一个有 10 个属性的模型。 A, B, C and so on... A、B、C 等等...
Property A
is an array.属性A
是一个数组。
For each value in array I generate one tag like this:对于数组中的每个值,我生成一个这样的标签:
<div class="col-sm-10 row">
@foreach (var item in Model.A)
{
<div class="col-sm-1 right-buffer">
<a href="@item.Value"><i class="" aria-hidden="true">@item.Text</i></a>
</div>
}
</div>
When user clicks on some link I should redirect it to the same page, but with Some model property changed.当用户单击某个链接时,我应该将其重定向到同一页面,但某些模型属性已更改。 For example:例如:
Current url: my/controller/someaction?name=Alex&age=20&from=fr&CurrentA=
with model ?name=Alex&age=20&from=fr&CurrentA=
当前网址: my/controller/someaction?name=Alex&age=20&from=fr&CurrentA=
with model ?name=Alex&age=20&from=fr&CurrentA=
If user clicks on <a>
with text foo
it should be redirected on my/controller/someaction?name=Alex&age=20&from=fr&CurrentA=foo
如果用户点击带有文本foo
<a>
它应该被重定向到my/controller/someaction?name=Alex&age=20&from=fr&CurrentA=foo
then is clicks on <a>
with text bar
and it should be now redirected on my/controller/someaction?name=Alex&age=20&from=fr&CurrentA=bar
然后就是网上的点击<a>
文字bar
,它现在应当对重定向my/controller/someaction?name=Alex&age=20&from=fr&CurrentA=bar
So entire query string (except one parameter) should be preserved to send current model state to server while I want to set one value and redirect it to the same page but with new value.因此,应保留整个查询字符串(一个参数除外)以将当前模型状态发送到服务器,而我想设置一个值并将其重定向到同一页面但具有新值。
Eventually, it should acts like postback with one extra value setted to model
最终,它应该像postback with one extra value setted to model
一样postback with one extra value setted to model
Is it possible or I should use JS and perform everything myself?是否有可能,或者我应该使用 JS 并自己执行所有操作?
Manually i solved it like this:我手动解决了这个问题:
First, create hidden fields for every property in model:首先,为模型中的每个属性创建隐藏字段:
<form asp-controller="search" asp-action="index" method="get" role="form" id="searchForm" asp-antiforgery="false">
<input asp-for="SessionId" type="hidden" name="sessionId" value="@Model.SessionId" />
<input asp-for="Quantity" type="hidden" name="quantity" value="@Model.Quantity"/>
<input asp-for="SortField" type="hidden" name="sortField" value="@Model.SortField"/>
<input asp-for="IsAscending" type="hidden" name="IsAscending" value="@Model.IsAscending" />
<input asp-for="Offset" type="hidden" name="offset" value="0" />
...
</form>
Then, use JS to replace value in hidden field and then submit form.然后,使用JS替换隐藏字段中的值,然后提交表单。 Values from inputs will be autimatically converter in query string, so everything works fine:来自输入的值将在查询字符串中自动转换,因此一切正常:
function sortDocuments(sortField) {
var sField = document.getElementById('SortField');
var isDescending = document.getElementById('IsAscending');
if (sField.value === sortField) {
if (isDescending.value.toUpperCase() === 'FALSE') {
isDescending.value = 'TRUE';
} else {
sField.value = 'rank';
isDescending.value = 'FALSE';
}
} else {
sField.value = sortField;
isDescending.value = 'FALSE';
}
document.getElementById('searchForm').submit();
}
Not very elegant, but it does its job.不是很优雅,但它完成了它的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.