Kuujoe 36
Kuujoe 36

Reputation: 41

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

can someone fix my code, I want to make variable for disable my textbox

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>

I want if item.status is 1, I can edit it, but if item.status is 2, textarea will disabled

Upvotes: 0

Views: 107

Answers (2)

Saineshwar Bageri - MVP
Saineshwar Bageri - MVP

Reputation: 4031

I have replicated the same View which you are facing. In this part i have used pure html control for generating textboxArea

Model

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; }       
}

View

@{
    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" />
}

Controller

 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);
    }

Upvotes: 0

Jibin Balachandran
Jibin Balachandran

Reputation: 3441

Check status and add disable property to the textarea.

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>
        }
    }    

If you have more texarea, then you could do something like:

 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>
            }
        }    

You can use ternary operator

  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>
            }
        }    

Upvotes: 1

Related Questions