Sunday 22 January 2017

MVC viewbag




//HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace model3.controller
{
    
    public class HomeController : Controller
    {
        // GET: /<controller>/
        public IActionResult Default()
        {
            ViewBag.cities = new List<string> { "victoria", "vancouver", "calgary", "saskatoon", "regina" };

            return View();
        }

        [HttpPost]
        public IActionResult Process()
        {

            var full_name = HttpContext.Request.Form["ux_fullname"];
            var phone = HttpContext.Request.Form["ux_phone"];
            var city = HttpContext.Request.Form["ux_city"];

            var message = $"welcome {full_name} from {city},to the MVC course";

            ViewBag.cities = new List<string> { "victoria", "vancouver", "calgary", "saskatoon", "regina" };
            ViewBag.Message = message;
            //TempData.Add("Message", message);

            return View("Default");
            //return RedirectToAction("Default");
        }
    }
}

--------------------------------------------------------------------

//default.cshtml

<h3>home page</h3>
welcome to mvc

<form method="post" action="~/Home/Process">
    <table>
        <tr>
            <td>FullName</td>
            <td><input name="ux_fullname" type="text"/></td>
        </tr>
        <tr>
            <td>Phone</td>
            <td><input name="ux_phone" type="text" /></td>
        </tr>
        <tr>
            <td>City</td>
            <td>
                <select name="ux_city">
                    @foreach (var city in ViewBag.cities)
                    {
                        <option value=@city>@city</option>

                    }
                    @*<option value="1">Calgary</option>
                    <option value="2">Saskatoon</option>
                    <option value="3">Edmonton</option>*@
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align:center">
                <input type="submit" value="Submit" />
            </td>
        </tr>
    </table>


</form>


<h4>@ViewBag.Message</h4>

@*<h4>t@TempData["Message"].ToString()</h4>*@

----------------------------------------------

//project.json

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",

    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

No comments:

Post a Comment