将每个表数据行显示为Label

时间:2016-03-28 08:36:08

标签: c# asp.net-mvc linq

我正在关注FinanceProductFeatures表,我希望将每个表记录显示为表单的标签名称。

enter image description here

所以我创建了这样的模型类

public class ProductFinanceFeatures
{
    public IList<AB_FinanceProductFeatures> ListProductFinanceFields { get; set; }
}
public partial class AB_FinanceProductFeatures
{
    public string ProductFinanceNameEn { get; set; }
    public string ProductFinance_Value_EN { get; set; }        
}

然后像这样的控制器类

    [HttpGet]
    public ViewResult Financing_Product_Feature_Configuration()
    {
        var model = new ProductFinanceFeatures
        {
            ListProductFinanceFields = db.FinanceProductFeatures.ToList()
        };

        return View(model);
    }

然后它的视图就像这样

@model albaraka.Models.ProductFinanceFeatures

@{

}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @for (int i = 0; i < Model.ListProductFinanceFields.Count; i++)
        {
            <div class="form-group">
                @Html.LabelFor(model => model.ListProductFinanceFields[i].ProductFinanceNameEn, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextAreaFor(m => m.ListProductFinanceFields[i].ProductFinance_Value_EN, new { @row = 5 })
                </div>
            </div>
        }

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

但是在这里我没有得到预期的结果,无法呈现标签

显示如下

enter image description here

1 个答案:

答案 0 :(得分:-1)

只需将TextAreaFor替换为DisplayFor,如下所示 -

<div class="col-md-10">
      @Html.DisplayFor(m => m.ListProductFinanceFields[i].ProductFinance_Value_EN, new { @row = 5 })
</div>

或者

<div class="col-md-10">
     @Html.DisplayTextFor(m => m.ListProductFinanceFields[i].ProductFinance_Value_EN)
</div>

希望这对你有用..!