在没有DB上下文的视图MVC5中显示模型信息

时间:2015-06-12 10:14:17

标签: c# asp.net-mvc asp.net-mvc-5

所以我是MVC5的新手,我正在创建一个不使用DB上下文的网站。如何将从字段中获取的模型信息显示到视图中?我不能使用DB上下文,这将使这更容易。

这是我的控制器:

using SaasSocialNetwork.Models;
using SSN.Shared.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using log4net;

namespace SaasSocialNetwork.Controllers
{
public class OpportunitiesController : Controller
{

    ILog logger = LogManager.GetLogger(typeof(OpportunitiesController));
    //
    // GET: /Opportunities/
    public ActionResult Index(OpportunityModel opportunity)
    {

        List<OpportunityModel> opportunities = new List<OpportunityModel>();
        opportunities.Add(opportunity);

        try
        {
             //multishops = TownBL.getMultishops().ToList();
             return View(opportunities);
        }
        catch(Exception ex)
        {
            logger.Error(ex);
            TempData["Warnning"] = ex.Message;
            return View(opportunities);
        }
        finally
        {
               logger.Debug("<<Index");
        }
        //return View();
    }


    // GET: /Opportunities/Details/5
    public ActionResult Details(OpportunityModel opportunity)
    {
        logger.Debug(">>Details");
        try
        {
            //MultiShops multishop = TownBL.getMultishops().Single(m => m.ID == id);
            //logger.InfoFormat("{0} Multi Shop Details", opportunity.Name);
            //var list = new List<string> { "test1", "test2", "test3" };
            return View("Details", opportunityModel);
        }
        catch (Exception ex)
        {
            logger.Error(ex);
            return View("Details");
        }
        finally
        {
            logger.Debug("<<Details");
        }
        return View();
    }


    // GET: /Opportunities/Create
    public ActionResult Create()
    {
        logger.Debug(">>Create(GET)");
        try
        {


            return View("Create");

        }
        catch (Exception ex)
        {
            logger.Error(ex);
            return View("Index");
        }
        finally
        {
            logger.Debug("<<Create");
        }
        //return View();
    }

    //
    // POST: /Opportunities/Create
    [HttpPost]
    public ActionResult Create(OpportunityModel opportunity)
    {
        try
        {
            // TODO: Add insert logic here
            OpportunityModel object1 = new OpportunityModel();
            SSN.Shared.Interfaces.Opportunity opp = new Opportunity();

            opp.Name = object1.Name;
            opp.Description = object1.Description;
            opp.ImageUrl = object1.ImageUrl;
            opp.GuidID = object1.GuidID;

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    //
    // GET: /Opportunities/Edit/5
    //public ActionResult Edit(Guid GuidId)
    //{
    //    logger.Debug(">>Edit(GET)");
    //    try
    //    {
    //        //MultiShops opportunity = TownBL.getMultishops().Single(m => m.ID == id);
    //        ViewBag.state = new List<SelectListItem>{

    //                         new SelectListItem { Text = "Active", Value = "Active" },
    //                         new SelectListItem { Text = "Suspended", Value ="Suspended"},
    //                    };
    //        //return View(opportunity);
    //    }
    //    catch (Exception ex)
    //    {
    //        logger.Error(ex);
    //        return View();
    //    }
    //    finally
    //    {
    //        logger.Debug("<<Edit");
    //    }
    //}

    //
    // POST: /Opportunities/Edit/5
    [HttpPost]
    public ActionResult Edit(OpportunityModel opportunity)
    {
        logger.Debug(">>Edit(GET)");
        try
        {
            if (ModelState.IsValid)
            {
                //TownBL.UpdateMultiShop(multishop);
                logger.InfoFormat("Opportunity {0} Updates", opportunity.Name);
            }

            TempData["Sucess"] = "Opportunity Sucessfully Updated";
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            logger.Error(ex);
            TempData["Warnning"] = "Error Updating Opportunity";
            return View();
        }
        finally
        {

            logger.Debug("<<Edit(POST)");
        }
    }

    //
    // GET: /Opportunities/Delete/5
    //public ActionResult Delete(Guid GuidId)
    //{
    //    logger.Debug(">>Delete(GET)");
    //    try
    //    {
    //        //MultiShops opportunity = TownBL.getMultishops().Single(m => m.ID == id);
    //        TempData["Sucess"] = "Opportunity Sucessfully Deleted";
    //        //return View(opportunity);
    //    }
    //    catch (Exception ex)
    //    {
    //        logger.Error(ex);
    //        TempData["Warnning"] = "Error Deletting Opportunity";
    //        return View("Index");
    //    }
    //    finally
    //    {
    //        logger.Debug("<<Delete(GET)");
    //    }
    //}

    //
    // POST: /Opportunities/Delete/5
    [HttpPost]
    public ActionResult Delete(OpportunityModel opportunity)
    {
        logger.Debug(">>Delete(POST)");
        try
        {

            //TownBL.DeleteMultiShop(multishop.ID);
            logger.InfoFormat("{0} Opportunity Deleted", opportunity.Name);
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            logger.Error(ex);
            return View();
        }
        finally
        {
            logger.Debug("<<Delete(POST)");
        }
    }
}

}

我的模特:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Security;


namespace SaasSocialNetwork.Models
{
    public class OpportunityModel
    {
        public Guid GuidID { get; set; }
        [Required(ErrorMessage = "Enter ID")]
        [StringLength(50)]
        [RegularExpression("^[0-9a-zA-Z ]+$", ErrorMessage = "ID must be alphanumeric")]
        public string Id { get; set; }

        [Required(ErrorMessage = "Enter Name")]
        [StringLength(50)]
        [RegularExpression("^[0-9a-zA-Z ]+$", ErrorMessage = "Name must be alphanumeric")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Enter Network ID")]
        [StringLength(50)]
        [RegularExpression("^[0-9a-zA-Z ]+$", ErrorMessage = "ID must be alphanumeric")]
        public string NetworkId { get; set; }

        [Required(ErrorMessage = "Enter Description")]
        public string Description { get; set; }

        [Required(ErrorMessage = "Enter image URL")]
        public string ImageUrl { get; set; }

        /// <summary>
        ///  This is the name of the organization
        /// </summary>
        [Required(ErrorMessage = "Enter the organization name")]
        [StringLength(50)]
        [RegularExpression("^[0-9a-zA-Z ]+$", ErrorMessage = "Name must be alphanumeric")]
        public string Organization { get; set; }

    }

}

我想填写的观点:

@model IEnumerable<SaasSocialNetwork.Models.OpportunityModel>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.GuidID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NetworkId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ImageUrl)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Organization)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.GuidID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NetworkId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ImageUrl)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Organization)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }

</table>

0 个答案:

没有答案