Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the changing of the property in the expression here is a red-herring - it won't work in either case.</p> <p><strong><em>Update</em></strong></p> <p>However, see at the end of my answer for some probably needlessly detailed exposition on why you didn't get an error first-time round.</p> <p><strong><em>End Update</em></strong></p> <p>You're using <code>ListBoxFor</code> - which is used to provide users with multiple selection capabilities - but you're trying to bind that to an <code>int</code> property - which cannot support multiple selection. (It needs to be an <code>IEnumerable&lt;T&gt;</code> at least to be able to bind a list box to it by default in MVC)</p> <p>I think you mean to be using <code>DropDownListFor</code> - i.e. to display a list of items from which only one can be selected?</p> <p>If you're actually looking for single-selection semantics in a listbox, that's trickier to do in MVC because it's Html helpers are geared entirely around listboxes being for multiple selection. Someone else on SO has asked a question about how to get a dropdown to look like a list box: <a href="https://stackoverflow.com/questions/351483/how-do-i-create-a-listbox-in-asp-net-mvc-with-single-selection-mode">How do I create a ListBox in ASP.NET MVC with single selection mode?</a>.</p> <p>Or you could generate the HTML for such a listbox yourself.</p> <p><strong><em>(Update) - Potentially needlessly detailed exposition(!)</em></strong></p> <p>The reason you don't get an exception first time round is probably because there was no value for <code>id</code> in <code>ModelState</code> when the HTML was generated. Here's the reflected MVC source (from <code>SelectExtensions.SelectInternal</code>) that's of interest (the <code>GetSelectListWithDefaultValue</code> call at the end is the source of your exception):</p> <pre><code>object obj = allowMultiple ? htmlHelper.GetModelStateValue(fullHtmlFieldName, typeof(string[])) : htmlHelper.GetModelStateValue(fullHtmlFieldName, typeof(string)); if (!flag &amp;&amp; obj == null &amp;&amp; !string.IsNullOrEmpty(name)) { obj = htmlHelper.ViewData.Eval(name); } if (obj != null) { selectList = SelectExtensions.GetSelectListWithDefaultValue(selectList, obj, allowMultiple); } </code></pre> <p>Note first that the control variable <code>allowMultiple</code> is true in your case, because you've called <code>ListBoxFor</code>. <code>selectList</code> is the <code>SelectList</code> you create and pass as the second parameter. One of the things that MVC (unfortunately in some cases) does is to use <code>ModelState</code> to modify the select list you pass when re-displaying a view in order to ensure that values which were set in <code>ModelState</code> via a POST are re-selected when the view is reloaded (this is useful when page validation fails because you won't copy the values to your underlying model from <code>ModelState</code>, but the page should still show those values as being selected).</p> <p>So as you can see on the first line, the model's current value for the expression/field you pass is fished out of model state; either as a string array or as a string. If that fails (returns <code>null</code>)then it makes another go to execute the expression (or similar) to grab the model value. If it gets a non-null value from there, it calls <code>SelectExtensions.GetSelectListWithDefaultValue</code>.</p> <p>As I say - what you're trying to do will ultimately not work in either the case of <code>Id</code> or <code>SupplierId</code> (because they would need to be <code>IEnumerable</code>) <strong>but</strong> I believe this <code>ModelState</code>-><code>Eval</code> process is yielding a null value when you use <code>Id</code>, so the process of getting an 'adjusted' <code>SelectList</code> is skipped - so the exception doesn't get raised. The same is not true when you use <code>SupplierId</code> because I'll wager that there's either a value in <code>ModelState</code> at that point, or the <code>ViewData.Eval</code> successfully gets an integer value.</p> <p><em>Not throwing an exception</em> is <strong>not</strong> the same as <strong><em>working</em></strong>!.</p> <p><strong><em>End update</em></strong></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