Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, passing a <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.formcollection%28v=vs.100%29.aspx" rel="nofollow">FormCollection</a> is basicly wrong. The <code>FormCollection</code> is used to iterate through all post-values of a form.</p> <p>I think your understanding of the basic approach of MVC is wrong. What you are trying to achive is editing a file. So your model should be a class called <code>FileModel</code>. The controller should look like this:</p> <pre><code>[HttpGet] public ActionResult Index() { return View("Index", new FileModel()); } </code></pre> <p>This provides a view, bound to a new instance of <code>FileModel</code>. In the next step you want to select the file name of the file you want to edit. This means, your <code>FileModel</code> will get an property, called <code>FileName</code>, which get's assigned in the next step.</p> <pre><code>public class FileModel { [Required] public string FileName { get; set; } } </code></pre> <p>We now have the <em>model</em> and the <em>controller</em>. Whats missing is the <em>view</em> part. This is the part that's actually responsible for user interaction. How to select a file is not the responsibility of the controller, so building up your drop-down should be done from the view. In my opinion the controller shouldn't even know that a drop-down is used to select a file name.</p> <pre><code>@model FileModel @using (Html.BeginForm()) { &lt;fieldset&gt; &lt;ol&gt; &lt;li&gt; @Html.LabelFor(m =&gt; m.FileName) @Html.DropDownListFor(m =&gt; m.FileName) @Html.ValidationMessageFor(m =&gt; m.FileName) &lt;/li&gt; &lt;/ol&gt; &lt;input type="submit" value="Edit file" /&gt; &lt;/fieldset&gt; } </code></pre> <p><code>DropDownListFor</code> simply tells the view, that the editor template for <code>FileName</code> is not a simple text box, but a drop-down. Now the last step is to fill in the options for the drop-down. Therefore we can use an overload of <code>DropDownListFor</code>.</p> <pre><code>@Html.DropDownListFor(m =&gt; m.FileName, new SelectList(Directory.GetFiles(@"C:\Something\")) </code></pre> <p>Now you should be able to select a file from your directory. This selection can be read in the controller from the post method:</p> <pre><code>[HttpPost] public ActionResult Index(FileModel model) { var selectedFileName = model.FileName; } </code></pre>
    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.
 

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