How to populate a Model in MVC
<p>So I am trying to populate a model, which needs a userId. So right now, my <code>ActionResult</code> <code>Index</code> method just returns a view which prompts for the user to enter their <code>userId</code>. I need to grab this value and then create the viewmodel and then pass it in back to the view so that way I can do stuff like <code>@Model.blah</code>. I was wondering how I can do that, whether it'd be having two different action result methods, or in general how to populate models when the information you need must be queried first before constructing the <code>viewmodel</code>. </p>

<p>Here is my controller: </p>

<pre><code> public ActionResult Index()
        {
            // Ask for UserID
            return View("~/Views/FingerprintTool/Index.cshtml");
        }


public ActionResult Index(int userId)
        {

            var response = _driver.ListFingerprints(userId);
            var model = new FingerprintToolModel()
            {
                Fingerprints = response.Fingerprints
            };
            return View(model);
        }
</code></pre>

<p>And here is my html: </p>

<pre><code>  model Models.Tools.FingerprintToolModel

&lt;head&gt;
    &lt;script type="text/javascript" src="~/Scripts/FingerprintTool.js"&gt;&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Fingerprint Tool&lt;/h1&gt;
&lt;form id="userIdForm" method="post"&gt;
    Type in your UserId: &lt;input name="userId" type="number" id="formUserId"/&gt;
    &lt;input type="submit"/&gt;
&lt;/form&gt;

    @if (Model != null)
    {
        &lt;h1&gt;SUCCESS&lt;/h1&gt;
    }
&lt;/body&gt;
</code></pre>

<p>I also have a Javascript file that deals with the submit button be clicked and whatnot. </p>

<p>Here is the Js:</p>

<pre><code>window.onload = function() {
    $("#userIdForm")
        .submit(function(e) {
            e.preventDefault();
            if ($("#formUserId").length == 0) {
                alert("Invalid UserId");
            } else {
                listUserFingerprints();
            }

        });
}

function listUserFingerprints() {
    // what to do here
}
</code></pre>