1

so I'm new to MVC5 and I'm creating a website without the use of a DB context. How do I go about displaying Model info taken from fields into a view? I cannot use a DB context which would've made this a whole lot easier.

Here's my Controller:

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)");
        }
    }
}

}

My Model:

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; }

    }

}

And the view I wish to populate:

@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>
ekad
  • 14,436
  • 26
  • 44
  • 46
  • Are you trying to create some interfaces to create edit and delete "opportunities" and have the index view display them, but without persisting this data to a database? Here was a good post on saving data in session variables: http://stackoverflow.com/questions/10756140/asp-net-mvc-and-state-how-to-keep-state-between-requests, though it will only work per session obviously so if you want long term storage of the "opportunities" or if you want one user to see an opportunity created by another user you'll have to store that data in a file or database. – Alec Menconi Jul 02 '15 at 20:45

0 Answers0