如何覆盖ASP.Net Core中的一个模型属性 [英] How to overwrite one model property in ASP.Net core

查看:80
本文介绍了如何覆盖ASP.Net Core中的一个模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有10个属性的模型. A,B,C等...

I have a model with say 10 properties. A, B, C and so on...

属性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:

当前网址:my/controller/someaction?name=Alex&age=20&from=fr&CurrentA= 型号为?name=Alex&age=20&from=fr&CurrentA=

如果用户单击带有文本foo<a>,则应将其重定向到my/controller/someaction?name=Alex&age=20&from=fr&CurrentA=foo

If user clicks on <a> with text foo it should be redirected on my/controller/someaction?name=Alex&age=20&from=fr&CurrentA=foo

然后单击文本为bar<a>,现在应该在my/controller/someaction?name=Alex&age=20&from=fr&CurrentA=bar

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

因此,应保留整个查询字符串(一个参数除外),以将当前模型状态发送到服务器,而我想设置一个值并将其重定向到同一页面,但使用新值.

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.

最终,它的行为应类似于postback with one extra value setted to model

Eventually, it should acts like postback with one extra value setted to model

有可能还是我应该使用JS自己执行所有操作?

Is it possible or I should use JS and perform everything myself?

推荐答案

我是这样手动解决的:

首先,为模型中的每个属性创建隐藏字段:

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>

然后,使用JS替换隐藏字段中的值,然后提交表单.输入的值将自动转换为查询字符串,因此一切正常:

Then, use JS to replace value in hidden field and then submit form. 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.

这篇关于如何覆盖ASP.Net Core中的一个模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆