0

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

4
  • I can't understand what you're trying. Are you want to disable by value in server-side or client-side? Note that disabled input prevents postback of its value. Commented Nov 20, 2017 at 6:33
  • @TetsuyaYamamoto server, if value in database is 1 or 2, like that Commented Nov 20, 2017 at 6:39
  • @Kuujoe36 i have created example using html control have a view on that Commented Nov 20, 2017 at 7:20
  • @Kuujoe36 i have posted answer is html one sir Commented Nov 20, 2017 at 8:31

2 Answers 2

1

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> } } 
Sign up to request clarification or add additional context in comments.

4 Comments

if I use like this.. if I have more textarea,. it will take a long code
You can put all the test areas inside the same if else
yah, but is to much, because I have 15 input text, If I used variable its simple more than that
Then use ternary operator
0

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

10 Comments

you just displaying data or this data will be posted
just display if item.status == 1 but, I used like this @Html.TextAreaFor(x => x.rol_tb_form1.permasalahan, new { @style = "width:98%", @rows = "3", @data }) , its error, @Saineshwar
this syntax will work i have removed '@' sign because of formatting issue [Html.TextAreaFor(m => m.rol_tb_form1.permasalahan, new { class = "form-control", disabled = data }) ] and change [data = "disabled='disabled'";] to data = "disabled";
I used that but if status == 2, I cant edit it, @Saineshwar
@Kuujoe36 sir you can use Html pure control will be good do not use helpers will work
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.