简体   繁体   中英

Creating multiple instances of model in asp.net mvc project

A little background of the project:

I am new in asp.net and are currently struggling with a school project, where i need to make a prototype for a GUI from where people can create/update/close a product-status, which will be send out to people on mail when there is a problem with a product. My idea is to split the screen vertically, where you on the left side will write what information there should be in the mail (product-status) and the right side displays a list of current product-statusses, from where you can choose to either update og close a given one.

Problem:

My problem is that i cannot find a way to make multiple instances of a model. I have created a test model, controller and view to find out how to do it and these will now be listed:

Below is my model, which will hold the information for the mail

public class ProductStatus {
        public string MailTitel { get; set; } // Could be "The casino game Bonanza is not working"
        public string Status { get; set; }    // Could be "Start", "Ongoing", "Ended"
        public string Description { get; set; } // Could be "Incident created at provider"
}

Below is my controller:

public class HomeController : Controller
    {
        List<ProductStatus> statusList = new List<ProductStatus> {};

        public ActionResult Index()
        {
            ProductStatus model = new ProductStatus();
            ViewData["Status"] = statusList;
            return View(model);
        }

        public ActionResult SendMessages(ProductStatus status) {
            statusList.Add(status);
            ViewData["Status"] = statusList;
            return View("Index");
        }
    }

To test my implementation i have created this view:

@{
    ViewBag.Title = "Productstatus";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using DS_Prototype.Models
@model DS_Prototype.Models.ProductStatus
<!--Left side - Info filling -->
<div class="split left">
    @using (Html.BeginForm("SendMessages", "Home", FormMethod.Post))
    {
        @Html.AntiForgeryToken()
        @Html.TextBoxFor(model => model.MailTitel)
        <button type="submit" class="btn btn-block btn-login">Submit</button>
    }
</div>

<!--Right side - Status overview -->
<div class="split right">
    @foreach (ProductStatus status in ViewData["Status"] as List<ProductStatus>)
    {
        <a>@status.MailTitel</a>
    }
</div>

When i fill in the left side and click the button, the correct string shows on the right side, but when i click the button again, it just updates the string and does not display a new string under the other string.

I know using ViewData is not the optimal way, but i could not get [httpPost] / [httpGet] to work (i think you can do it using these in some way).

I hope someone can help me, thanks in advance!

If I understand this correctly, the first time you enter a value into the textbox and click submit, that value shows up in the right side of your page. When you put a different value into the box and submit again, you want the right side to show both the first and second values. Is that correct?

Assuming that's what you want, then the thing to remember is that HTTP is stateless, meaning that every time you make a new request, you are starting from scratch. So when you submit the form a second time, the page is not going to remember that first entry. ViewData does not persist between requests, and neither does your statusList because it's created new every time the controller is created (which is every request).

Your best bet is to use Session to hold the previously submitted values.

public class HomeController : Controller
{

    public ActionResult Index()
    {
        ProductStatus model = new ProductStatus();
        List<ProductStatus> statusList = (List<ProductStatus>)Session["ProductStatuses"];
        if (statusList != null)
            ViewData["Status"] = statusList;
        return View(model);
    }

    public ActionResult SendMessages(ProductStatus status) 
    {
        List<ProductStatus> statuses = (List<ProductStatus>)Session["ProductStatuses"];
        if (statuses == null)
        {
             statuses = new List<ProductStatus>();
             Session["ProductStatuses"] = statuses;
        }
        statusList.Add(status);
        ViewData["Status"] = statusList;
        return View("Index");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM