Category: asp-net mvc

Dec 30 2015

Using Yeoman to scaffold asp.net mvc applications

  1. Install nodejs
  2. From the node command prompt install yeoman ... npm  install -g yo
  3. You may want to install any dependencies that will be used by yeoman like...
    npm install -g gulp
    npm install -g bower
    npm install -g grunt
    npm install -g grunt-cli
  4. Install a yeoman generator...for example
    npm install -g generator-aspnet
    or (for a plain vanilla website)
    npm install -g generator-webapp
  5. Create and navigate to the target directory where you want to create your aspnet web app.
  6. run a yeoman generator as follows...
    yo aspnet
    or
    yo (then select your generator from the list)
  7. When you run the aspnet generator you will be prompted to name your web app...name it.
  8. The navigator will create a folder matching your app name...navigate into that folder (cd <appname>)
  9. To create a dotnet runtime environment for your app type...
    dnu restore
    If you get a dnu command not found error ... from another admin cmd type ...
    dnvm upgrade...
    If that command doesn't work see this page...
    https://github.com/aspnet/home/
  10. To launch the web server type dnx web
  11. To get help on a generator type yo <generatorname> --help...like this...
    yo aspnet  --help
  12. This will show you all of the  sub-generators for the generator
    1. To get help on a subgenerator type yo <generator>:<subgen> --help ... like this...
      yo aspnet:TypeScript --help
      or
      yo aspnet:MvcController --help
  13. So..to generator a new controller do yo aspnet:MvcController <controllername>...like this...
    yo aspnet:MvcController HelpController
  14. To create a matching view for an action you can use aspnet:MvcView <viewname>...BUT...
    first you have to navigate to the views folder (cd views) create a target folder that matches the controller name ... like "Help" and then navigate to that folder.

For more information on this, including how it all works beautifully with visual studio code see this...
https://code.visualstudio.com/Docs/runtimes/ASPnet5

Oct 24 2012

How do I get files AND form values from an ASP.NET MVC 4 website?

I have an ASP.NET MVC Website that has a dropdown list that is being created using this in the view...

@Html.DropDownList("Programs")

Programs is populated from a Business Object collection and stuffed into the ViewBag in the index action on the Home Controller...

\\get items...
ViewBag.Programs = items;

The view also has potentially three files I am getting like this in the same view...

<input type="file" name="files" id="txtUploadPlayer" size="40" />  
<input type="file" name="files" id="txtUploadCoaches" size="40" />  
<input type="file" name="files" id="txtUploadVolunteers" size="40" /> 

All of the aforementioned controls are contained in a Form that is created in the view using...

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
     <!--  file and other input types  -->
     <input type="submit" name="btnSubmit" value="Import Data" />
}

My problems is that I cannot find a way to process all of the files AND reference the form fields.

Specifically, I need to know what Program the user selected from the dropdown.

I can process the files using this code with no problem...

[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files)
//public ActionResult Index(FormCollection form)
{

    _tmpFilePath = Server.MapPath("~/App_Data/uploads");

    if (files == null) return RedirectToAction("Index");
    foreach (var file in files)
    {
        if (file != null && file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(_tmpFilePath, fileName);
            if (System.IO.File.Exists(path)) System.IO.File.Delete(path);

            _file = file;

            file.SaveAs(path);

            break;  //just use the first file that was not null.
        }
    }



    //SelectedProgramId = 0;

    //DoImport();

    return RedirectToAction("Index");
}

But I cannot figure how to ALSO get access to the POST form values especially the Programs dropdown selected value (and for the record there is also a checkbox that I cannot read the value from.) Fiddler shows me that the Response has the file references AND the selected program but I cannot figure out how to get them out of the POST using ASP.NET MVC.

I know this question is pretty basic but I am stilling learning the whole web/http thing not just MVC.

EDIT Thanks for your answers. I had the thought that the answer might lie in passing in both the files and the form values into the POST.

So my last question is ... how do I change the HTML.BeginForm block to pass in both the files and form values? Right now I have ...

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  //do stuff
}

What should that using statement be to get both form values and files as separate parameters of the ActionResult?

EDIT MY EDIT
It seems that I don't have to make any changes...the debugger is showing that both files and form are non-null. Cool! Is that right?

May 16 2012

What is the use case for using a JsonResult action in asp.net mvc3?

When is it typical to use the JsonResult action in an ASP.NET MVC 3 application?

From where is the JsonResult usually called; from another action or an actionlink rendered in the html?

Can you give me some examples where you want json instead of a typical view?

May 16 2012

What is the use case for using a JsonResult action in asp.net mvc3?

When is it typical to use the JsonResult action in an ASP.NET MVC 3 application?

From where is the JsonResult usually called; from another action or an actionlink rendered in the html?

Can you give me some examples where you want json instead of a typical view?