MVC问题,请帮助我解决 [英] MVC problem, please help me to resolve

查看:99
本文介绍了MVC问题,请帮助我解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用强类型视图时,发生资源未找到错误
请帮助我解决这个问题..

几天前,我安装了mvc 3工具更新.为什么会这样呢?我的计算机是否存在任何故障,或者我的VS10的某些文件已损坏?

本地主机:1590/Logins/NewLogin

告诉我我的错在哪里? :(
有我的代码

-----------------------
型号:
----------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace SimpleLogin.Models
{
    public class LoginModel
    {
        private string user_ID;
        [Required]
        [DisplayName("User ID:")]
        
        public string User_ID
        {
            get { return user_ID; }
            set { user_ID = value; }
        }
        private string password;
        [Required]
        [DisplayName("Password:")]
        [DataType(DataType.Password)]

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

    }
}



------------------------------
控制器:
-----------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimpleLogin.Models;
namespace SimpleLogin.Controllers
{
    public class LoginsController : Controller
    {
        //
        // GET: /Logins/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult NewLogin(LoginModel m)
        {
            if (m.User_ID == "admin" && m.Password == "pass") 
            {
            
            }
            return View();
        }
    }
}


---------------------
查看:
--------------------

@model SimpleLogin.Models.LoginModel

@{
    ViewBag.Title = "NewLogin";
}

@using (Html.BeginForm())
{ 
    <table>
        <tr>
            <td>
                @Html.LabelFor(x=>x.User_ID)
            </td>
            <td>
                @Html.TextBoxFor(x=>x.User_ID)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(x=>x.Password)
            </td>
            <td>
                @Html.TextBoxFor(x=>x.Password)
            </td>
        </tr>
    </table>
}

解决方案

在您的控制器中,您需要为NEWLOGIN Action添加HTTPGET方法
如下更改控制器...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimpleLogin.Models;
namespace SimpleLogin.Controllers
{
    public class LoginsController : Controller
    {
        //
        // GET: /Logins/

        public ActionResult Index()
        {
            return View();
        }
	[HttpGET]
        public ActionResult NewLogin()
        {
            return View();
        }
        [HttpPost]
        public ActionResult NewLogin(LoginModel m)
        {
            if (m.User_ID == "admin" && m.Password == "pass") 
            {
            
            }
            return View();
        }
    }
}


尝试以下操作:

 [AllowAnonymous]
[HttpPost]
公共 ActionResult NewLogin(LoginModel m)
 {
    如果(m.User_ID == "  span>& m.Password == " )
    {

    }
   返回 View();
 } 


了解更多详情:
http://msdn.microsoft.com/zh-cn/library/system.web.http.allowanonymousattribute%28v = vs.108%29.aspx [ http://博客. msdn.com/b/rickandy/archive/2011/05/02/securing-your-asp-net-mvc-3-application.aspx [



------------------------------
Controller :
-----------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimpleLogin.Models;
namespace SimpleLogin.Controllers
{
    public class LoginsController : Controller
    {
        //
        // GET: /Logins/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult NewLogin(LoginModel m)
        {
            if (m.User_ID == "admin" && m.Password == "pass") 
            {
            
            }
            return View();
        }
    }
}


---------------------
View :
--------------------

@model SimpleLogin.Models.LoginModel

@{
    ViewBag.Title = "NewLogin";
}

@using (Html.BeginForm())
{ 
    <table>
        <tr>
            <td>
                @Html.LabelFor(x=>x.User_ID)
            </td>
            <td>
                @Html.TextBoxFor(x=>x.User_ID)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(x=>x.Password)
            </td>
            <td>
                @Html.TextBoxFor(x=>x.Password)
            </td>
        </tr>
    </table>
}

In your Controller you need to add HTTPGET method for NEWLOGIN Action
Change your controller like below...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimpleLogin.Models;
namespace SimpleLogin.Controllers
{
    public class LoginsController : Controller
    {
        //
        // GET: /Logins/

        public ActionResult Index()
        {
            return View();
        }
	[HttpGET]
        public ActionResult NewLogin()
        {
            return View();
        }
        [HttpPost]
        public ActionResult NewLogin(LoginModel m)
        {
            if (m.User_ID == "admin" && m.Password == "pass") 
            {
            
            }
            return View();
        }
    }
}


Try with following:

[AllowAnonymous]
[HttpPost]
public ActionResult NewLogin(LoginModel m)
 {
    if (m.User_ID == "admin" && m.Password == "pass")
    {

    }
   return View();
 }


Read more detail:
http://msdn.microsoft.com/en-us/library/system.web.http.allowanonymousattribute%28v=vs.108%29.aspx[^]
http://blogs.msdn.com/b/rickandy/archive/2011/05/02/securing-your-asp-net-mvc-3-application.aspx[^]


这篇关于MVC问题,请帮助我解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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