繁体   English   中英

ASP.NET MVC 视图计算列

[英]ASP.NET MVC view calculate for a column

I have a view model, I have a list from a table in my view model, I'm binding the model to my view and bind the list to an html table.

查看 model:

public IEnumerable<MyTable> TableObject { get; set; }

看法:

@if (Model.TableObject != null)
{
        foreach(var item in Model.TableObject)
        {
            <td>@item.Column1</td> 
            <td>@item.Column2</td>
            <td>CalculatedVariable</td>
        }   
}

我可以获取所有表格列值,例如 Column1、Colum2。 但我想计算一个显示值。 我可以用这样的方法在 controller 中得到这个值:

Controller:

public string GetCalculateValue(List<MyTable> searchList, int compareValue)
{
    string returnValue = String.Empty;

    var _theList = searchList.Where(x => x.myValue == compareValue).ToList();

    if (_theList.Count() > 1)
    {
        returnValue = "OK";
    }

    return returnValue;
}

我想将该 returnValue 绑定到我的视图以显示在 html 表的列中。 如何为我的表格行传递这个字符串值? 我希望我能解释一下。

感谢帮助。

很难看到发生了什么,但我相信你的一般方法需要如下。 向您的MyTable class 添加一个属性,使其看起来像这样

public class MyTable
{
    public string Column1 {get; set;}
    public string Column2 {get; set;}
    public int myValue {get; set;}
    public string CalculatedVariable {get; set;}
}

然后 foreach MyTable object 您已在 controller 中设置了 CalculatedVariable 的值。 然后,您将能够像其他属性一样显示计算变量。

不是你问的,但我认为如果将GetCalculateValue更改为此,它可以变得更具可读性。

public string GetCalculateValue(List<MyTable> searchList, int compareValue)
{
    return searchList.Any(x => x.myValue == compareValue) ? "OK" : "";          
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM