将用户添加到列表中的MVC [英] Adding users to a list MVC

查看:83
本文介绍了将用户添加到列表中的MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在,用户应该输入名称工资并单击按钮添加一个表单。当点击按钮添加,即假设用户的列表上显示。

这是我尝试过。

控制器:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;
使用TimeIsMoney.Models;命名空间TimeIsMoney.Controllers
{
    公共类HomeController的:控制器
    {        清单<&的usermodel GT;用户=新的List<&的usermodel GT;();
        公众的ActionResult指数(字符串RETURNURL)
        {
            ViewBag.ReturnUrl = RETURNURL;
            返回查看();
        }        公众的ActionResult ADDUSER(用户的usermodel)
        {
            users.Add(用户);
            返回查看(用户);
        }
    }
}

查看:

  @model TimeIsMoney.Models.LoginModel
@ {}
@功能{
    公共字符串GetAntiForgeryToken()
    {
        串cookieToken,formToken;
        AntiForgery.GetTokens(NULL,出cookieToken,出formToken);
        返回cookieToken +:+ formToken;
    }
}< D​​IV ID =主要内容级=COL-MD-8 COL-MD-偏移2>
    < D​​IV CLASS =COL-MD-12行>
        < H1>时间就是金钱,我的朋友<!/ H1>
    < / DIV>    < D​​IV CLASS =COL-MD-12行>
        < H2> 1000kr< / H>
    < / DIV>    < D​​IV CLASS =COL-MD-12行>
        <按钮类=BTN的onclick =开始()>开始< /按钮>
        <按钮类=BTN的onclick =reset()的>复位< /按钮>
    < / DIV>    < D​​IV CLASS =COL-MD-12行>
        <形式为GT;
            <输入类型=文本占位符=名称/>
            <输入类型=数字占位符=计时工资/>
            <输入类型=提交值=添加的onclick =ADDUSER()/>
        < /表及GT;
    < / DIV>    < D​​IV CLASS =COL-MD-12行>
        < D​​IV CLASS =COL-MD-3 COL-MD-偏移1>
            <标签>名称:LT; /标签>
            < UL>
                <立GT; Dave和LT; /李>
                <立GT;&皮特LT; /李>
            < / UL>
        < / DIV>
        < D​​IV CLASS =COL-MD-4>
            <标签>工资:LT; /标签>
            < UL>
                <立GT; 500KR / H< /李>
                <立GT; 500KR / H< /李>
            < / UL>
        < / DIV>
    < / DIV>
    < BR />
    < BR />
< / DIV>

型号:

 命名空间TimeIsMoney.Models
{
    公共类的usermodel
    {
        [需要]
        [数据类型(DataType.Text)
        [显示名称(用户名)]
        公共字符串用户名{获得;组; }        [需要]
        [数据类型(DataType.Text)
        [显示名称(工资)]
        公共字符串工资{搞定;组; }
    }
}

我在正确的道路上?
我怎样才能从这里?

更新:

 公众的ActionResult ADDUSER(用户的usermodel)
    {
        VAR列表=会话[myUsers]作为名单<的usermodel取代;
        list.Add(用户);
        返回查看(名单);
    }


解决方案

你主要是在正确的道路排除方法你要存储用户列表。

由于ASP.NET MVC控制器实例为每个请求创建和处置视图渲染并传递到浏览器之后 - 这将是新的控制器拿着新的列表<的usermodel> 在每次请求创建的。

所以,你必须将它存储在别处(会话变量,在服务器上的磁盘,数据库文件等)。通常数据库是最好的选择这一点。

在您想将其存储在会话变量的情况下,你应该增加像这样成的Global.asax

 保护无效在session_start(对象发件人,EventArgs的发送)
{
    会话[myUsers] =新的List<&的usermodel GT;();
}

,然后在你的控制器的方法,你就可以访问此列表为

  VAR列表=会话[myUsers]作为名单<的usermodel取代;

I have a form in which the user is supposed to enter Name and Wage and click a button Add. When the button "Add" is clicked, that user is supposed to be displayed on a list.

This is how I tried it.

Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeIsMoney.Models;

namespace TimeIsMoney.Controllers
{
    public class HomeController : Controller
    {

        List<UserModel> users = new List<UserModel>();
        public ActionResult Index(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        public ActionResult AddUser(UserModel user) 
        {
            users.Add(user);
            return View(users);
        }
    }
}

View:

@model TimeIsMoney.Models.LoginModel
@{

}
@functions{
    public string GetAntiForgeryToken()
    {
        string cookieToken, formToken;
        AntiForgery.GetTokens(null, out cookieToken, out formToken);
        return cookieToken + ":" + formToken;                
    }
}



<div id="main-content" class="col-md-8 col-md-offset-2">
    <div class="col-md-12 row">
        <h1>Time is money my friend!</h1>
    </div>

    <div class="col-md-12 row">
        <h2>1000kr</h2>
    </div>

    <div class="col-md-12 row">
        <button class="btn" onclick="start()">Start</button>
        <button class="btn" onclick="reset()">Reset</button>
    </div>

    <div class="col-md-12 row">
        <form >
            <input type="text" placeholder="Name" />
            <input type="number" placeholder="Hourly wage" />
            <input type="submit" value="Add" onclick="AddUser()" />
        </form>
    </div>

    <div class="col-md-12 row">
        <div class="col-md-3 col-md-offset-1">
            <label>Name:</label>
            <ul>
                <li>Dave</li>
                <li>Pete</li>
            </ul>
        </div>
        <div class="col-md-4">
            <label>Wage:</label>
            <ul>
                <li>500kr/h</li>
                <li>500kr/h</li>
            </ul>
        </div>
    </div>


    <br />
    <br />






</div>

Model:

namespace TimeIsMoney.Models
{
    public class UserModel
    {
        [Required]
        [DataType(DataType.Text)]
        [DisplayName("Username")]
        public string UserName { get; set; }



        [Required]
        [DataType(DataType.Text)]
        [DisplayName("Wage")]
        public string Wage { get; set; }
    }


}

Am I on the right path? How can I move on from here?

UPDATE:

public ActionResult AddUser(UserModel user) 
    {
        var list = Session["myUsers"] as List<UserModel>;
        list.Add(user);
        return View(list);
    }

解决方案

You're mostly on the right path excluding way you're trying to store your users list.

Since ASP.NET MVC controller instance is created for every request and disposed after view is rendered and passed to the browser - it will be new controller holding new List<UserModel> created on every request.

So you have to store it somewhere else (session variables, file on server's disk, database and so on). Usually database is best choice for this.

In the case you want to store it in session variable, you should add something like this into Global.asax:

protected void Session_Start(object sender, EventArgs e)
{
    Session["myUsers"] = new List<UserModel>();
}

and then in your controller's methods you will be able to access this list as

var list = Session["myUsers"] as List<UserModel>;

这篇关于将用户添加到列表中的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆