繁体   English   中英

我如何创建变量并在razor asp.net mvc视图4中声明

[英]how I creat variable and declare that in view razor asp.net mvc 4

有人可以修复我的代码吗,我想使变量禁用我的文本框

foreach (var item in Model.rol_tb_approve1)
    {
        if (Model.rol_tb_form1.id == item.id_form)
        {
            if (item.status == 1)
            {
                <text>
                @{
                var new = "disabled";
                }
                </text>
            }
        }
    }    

<div>
    <h3>I. Permasalahan<h3>

    @Html.TextAreaFor(x => x.rol_tb_form1.permasalahan, new { @style = "width:98%", @rows = "3", @new })
</div>

我想要item.status为1时,我可以对其进行编辑,但是如果item.status为2,则textarea将被禁用

检查status并将禁用属性添加到文本区域。

foreach (var item in Model.rol_tb_approve1)
    {
        if (Model.rol_tb_form1.id == item.id_form)
        {
          <div>
          <h3>I. Permasalahan<h3>
            if (item.status == 1)
            {
              @Html.TextAreaFor(x => x.rol_tb_form1.permasalahan, new { @style = "width:98%", @rows = "3"})
            }
            else
            {
              @Html.TextAreaFor(x => x.rol_tb_form1.permasalahan, new { @style = "width:98%", @rows = "3", @readonly = "readonly" })
            }
          </div>
        }
    }    

如果您有更多的texarea,则可以执行以下操作:

 foreach (var item in Model.rol_tb_approve1)
        {
            if (Model.rol_tb_form1.id == item.id_form)
            {
              <div>
              <h3>I. Permasalahan<h3>
                if (item.status == 1)
                {
                  @Html.TextAreaFor(x => x.rol_tb_form1.permasalahan, new { @style = "width:98%", @rows = "3",id="firsttextarea"})
                  @Html.TextAreaFor(x => x.rol_tb_form1.permasalahan, new { @style = "width:98%", @rows = "3",id="secondtextarea"})
                  @Html.TextAreaFor(x => x.rol_tb_form1.permasalahan, new { @style = "width:98%", @rows = "3",id="thirdtextarea"})
                }
                else
                {
                  @Html.TextAreaFor(x => x.rol_tb_form1.permasalahan, new { @style = "width:98%", @rows = "3", @readonly = "readonly",id="firsttextarea" })
                  @Html.TextAreaFor(x => x.rol_tb_form1.permasalahan, new { @style = "width:98%", @rows = "3", @readonly = "readonly",id="secondtextarea" })
                  @Html.TextAreaFor(x => x.rol_tb_form1.permasalahan, new { @style = "width:98%", @rows = "3", @readonly = "readonly",id="thirdtextarea" })
                }
              </div>
            }
        }    

您可以使用三元运算符

  foreach (var item in Model.rol_tb_approve1)
        {
            if (Model.rol_tb_form1.id == item.id_form)
            {
              <div>
              <h3>I. Permasalahan<h3>
                  @Html.TextAreaFor(x => x.rol_tb_form1.permasalahan,(item.status == 1)? new { @style = "width:98%", @rows = "3" }: {@style = "width:98%", @rows = "3", @readonly = "readonly"})
              </div>
            }
        }    

我已经复制了您面对的相同视图。 在这一部分中,我使用了纯HTML控件来生成textboxArea

模型

public class Demomodel
{
    public List<rol_tb_approve1> rol_tb_approve1 { get; set; }
    public rol_tb_form1 rol_tb_form1 { get; set; }
}

public class rol_tb_approve1
{
    public string id_form { get; set; }
    public int status { get; set; }
}

public class rol_tb_form1
{
    public string id { get; set; }
    public string permasalahan { get; set; }       
}

视图

@{
    Layout = null;
}
@model MvcApplication1.Models.Demomodel
@using (Html.BeginForm())
{

    var data = "";
    foreach (var item in Model.rol_tb_approve1)
    {
        if (Model.rol_tb_form1.id == item.id_form)
        {
            if (item.status == 1)
            {
    <text>
    @{
                data = "disabled='disabled'";
    }
    </text>
            }
        }
    }

    <div>
        <h3>
            I. Permasalahan<h3>
                <textarea name="@Model.rol_tb_form1.permasalahan" @data style="width:250px;height:150px;"></textarea>
    </div>
    <input id="Submit1" type="submit" value="submit" />
}

调节器

 public ActionResult Index()
    {

        Demomodel demomodel = new Models.Demomodel();

        rol_tb_approve1 rol_tb_approve1 = new rol_tb_approve1();
        rol_tb_approve1.id_form = "1";
        rol_tb_approve1.status = 0;

        rol_tb_form1 rol_tb_form1 = new rol_tb_form1();
        rol_tb_form1.id = "1";
        rol_tb_form1.permasalahan = "permasalahan";


        List<rol_tb_approve1> li = new List<Models.rol_tb_approve1> ();
        li.Add(rol_tb_approve1);

        demomodel.rol_tb_approve1 = li;
        demomodel.rol_tb_form1 = rol_tb_form1;

        return View(demomodel);
    }

暂无
暂无

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

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