Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are (at least) two Methods to do this:</p> <h3>Method 1: The Simple Way</h3> <p>You would do this by adding logic in your view:</p> <pre><code>&lt;input type="button" disabled=&lt;%= Model.number &lt;= 0 %&gt; /&gt; </code></pre> <p>Where <code>Model.number</code> is the count of items passed to your view by your controller. If it's less than or equal to zero, <code>disabled</code> will be <code>true</code>.</p> <p>The syntax may not be exact, I haven't tried this, but this is the path I would go down to do what you want.</p> <p>This will work for the initial setting of the value; changing it without refreshing the page is a matter of using JavaScript, as other answers have pointed out.</p> <h3>Method 2: The overly complex but more 'MVC' way</h3> <p>If you want the logic in the controller rather than the view, you should set up a specific ViewModel object that you can add the logic to:</p> <h3>ViewModel</h3> <pre><code>public class MyObjectViewModel { MyObject MyObject {get; private set; } bool Enabled; {get; set; } public MyObjectViewModel(MyObject obj) { MyObject = obj; } string IsEnabled { get { if (Enabled) { return ""; } else return "disabled=disabled"; } } </code></pre> <h3>Controller</h3> <pre><code>public ActionResult Show(int id) { MyObject myObject = repository.GetMyObjectById(id) MyObjectViewModel movm = myObject; movm.Enabled = myObject.number &gt; 0; return View(movm); } </code></pre> <h3>View</h3> <pre><code>&lt;input type="button" &lt;%= Model.IsEnabled %&gt; /&gt; </code></pre> <p>Again, the syntax and usage may be a little off, I'm prototyping this off the top of my head, and am not in a location where I can test this for you.</p> <p>If you're interested in ViewModels, here are some good resources:</p> <ul> <li><a href="https://stackoverflow.com/questions/664205/viewmodel-best-practices">View Model Best Practices</a></li> <li><a href="http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx" rel="nofollow noreferrer">ASP.NET MVC Tip# 50: Create View Models</a></li> </ul> <p>I've updated it to return <code>disabled=disabled</code> using the string if it is actually disabled.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload