MVC在网页上填写表单时丢失数据

时间:2016-06-28 07:54:12

标签: c# controller

问题是每当我点击提交按钮时,我的程序就会忘记用户写入文本框的所有数据。 " Ressurs"列表也永远不会高于2的数量。

我的控制器:

    const string SESSION_SAVED_MODEL = "savedModel";

    [HttpGet]
    public ActionResult Index()
    {

        Session[SESSION_SAVED_MODEL] = new EmailFormModel();


        return View(Session[SESSION_SAVED_MODEL]);
    }




    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index(EmailFormModel model)
    {



        if (Request.Params["Ekstra_Ressurs"] != null)
        {
            model.Ressurs.Add(new EmailFormModel.RessursBehov());
            Session[SESSION_SAVED_MODEL] = model;

        }

        return View(model);

    }
 }
 }

我的模特:

public class EmailFormModel
{
    public EmailFormModel()
    {
        Ressurs = new List<RessursBehov>() { new RessursBehov() };
    }


    public List<RessursBehov> Ressurs;

    public class RessursBehov
    {

        [Required, Display(Name = "Antall timer")]
        public int? Antall_Timer { get; set; }

        [Required, Display(Name = "Antall uker")]
        public int? Antall_Uker { get; set; }
    }
}
}

我的索引(在Views / app中):

索引

@model Debug.Models.EmailFormModel


@using (Html.BeginForm())
{

@Html.AntiForgeryToken()

@for (var i = 0; i < Model.Ressurs.Count; i++)
{


            <div class="row">
                <div class="col-md-4"></div>
                @Html.LabelFor(m => Model.Ressurs[i].Antall_Timer, new { @class = "col-md-1 text-center" })
                <div class="col-md-7">
                    @Html.TextBoxFor(m => Model.Ressurs[i].Antall_Timer, new { @class = "html_label" })


                </div>
            </div>

            <div class="row">
                <div class="col-md-4"></div>
                @Html.LabelFor(m => Model.Ressurs[i].Antall_Uker, new { @class = "col-md-1 text-center" })
                <div class="col-md-7">
                    @Html.TextBoxFor(m => Model.Ressurs[i].Antall_Uker, new { @class = "html_label" })

                </div>
            </div>
}

<div class="col-md-5"></div>
<div class="col-md-2">
    <input type="submit" class="btn btn-default" value="Legg til ressurser" name="Ekstra_Ressurs" />
</div>
}

1 个答案:

答案 0 :(得分:0)

由于某些原因使它成为IList自己的作品。 我会再研究一下,但现在是一个有效的解决方案: 首先是控制器

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        const string SESSION_SAVED_MODEL = "savedModel";

        [HttpGet]
        public ActionResult Index()
        {

            Session[SESSION_SAVED_MODEL] = new List<RessursBehov> { new RessursBehov() };


            return View(Session[SESSION_SAVED_MODEL]);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(IList<RessursBehov> model)
        {



             if (Request.Params["Ekstra_Ressurs"] != null)
            {
                model.Add(new RessursBehov());
                Session[SESSION_SAVED_MODEL] = model;

            }

            return View(model);

        }

    }

    public class RessursBehov
    {

        [Required, Display(Name = "Antall timer")]
        public int? Antall_Timer { get; set; }

        [Required, Display(Name = "Antall uker")]
        public int? Antall_Uker { get; set; }

    }
}

然后是视图

@model IList<WebApplication1.Controllers.RessursBehov>

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{

    @Html.AntiForgeryToken()

    for (var i = 0; i < Model.Count; i++)
    {
        <div class="row">
            <div class="col-md-4"></div>
            @Html.LabelFor(m => Model[i].Antall_Timer, new { @class = "col-md-1 text-center" })
            <div class="col-md-7">
                @Html.TextBoxFor(m => Model[i].Antall_Timer, new { @class = "html_label" })


            </div>
        </div>

        <div class="row">
            <div class="col-md-4"></div>
            @Html.LabelFor(m => Model[i].Antall_Uker, new { @class = "col-md-1 text-center" })
            <div class="col-md-7">
                @Html.TextBoxFor(m => Model[i].Antall_Uker, new { @class = "html_label" })

            </div>
        </div>
    }
    <div class="col-md-5"></div>
    <div class="col-md-2">
        <input type="submit" class="btn btn-default" value="Legg til ressurser" name="Ekstra_Ressurs" />
    </div>
}