【问题标题】:The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String]传入字典的模型项的类型为 'System.Collections.Generic.List`1[System.String]
【发布时间】:2014-04-21 12:33:15
【问题描述】:

我是第一次使用 MVC4 编程。

我有一个简单的文本框,想从 Controller 访问它。

我完成了以下操作:

家庭控制器:

 [HttpPost]
        public ActionResult   getValues(ModelClass mClass)
        {
            mClass.userName = "Hi!!, I am Sagar";
            return Content(mClass.userName);
        }

Index.cshtml:

@model  KendoUIMvcApplication.Models.ModelClass

@using (Html.BeginForm("Index", "Home", FormMethod.Post, null))
    {
        @Html.TextBoxFor(model=>model.userName.ToString())

    }

模型类:

namespace KendoUIMvcApplication.Models
{
    public class ModelClass
    {
        public string userName
        {
            get;
            set;
        }
    }
}

它给了我错误:

传入字典的模型项是类型 'System.Collections.Generic.List`1[System.String]',但这 字典需要类型的模型项 'KendoUIMvcApplication.Models.ModelClass'。

新错误:

视图“嗨!!,我是 Sagar”或其主人未找到,或者没有视图引擎支持搜索的位置。搜索了以下位置: ~/Views/Home/嗨!!,我是 Sagar.aspx ~/Views/Home/嗨!!,我是 Sagar.ascx ~/Views/Shared/嗨!!,我是 Sagar.aspx ~/Views/Shared/嗨!!,我是 Sagar.ascx ~/Views/Home/嗨!!,我是 Sagar.cshtml ~/Views/Home/嗨!!,我是 Sagar.vbhtml ~/Views/Shared/嗨!!,我是 Sagar.cshtml ~/Views/Shared/嗨!!,我是 Sagar.vbhtml

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.InvalidOperationException:未找到视图“嗨!!,我是 Sagar”或其主人,或者没有视图引擎支持搜索的位置。搜索了以下位置: ~/Views/Home/嗨!!,我是 Sagar.aspx ~/Views/Home/嗨!!,我是 Sagar.ascx ~/Views/Shared/嗨!!,我是 Sagar.aspx ~/Views/Shared/嗨!!,我是 Sagar.ascx ~/Views/Home/嗨!!,我是 Sagar.cshtml ~/Views/Home/嗨!!,我是 Sagar.vbhtml ~/Views/Shared/嗨!!,我是 Sagar.cshtml ~/Views/Shared/嗨!!,我是 Sagar.vbhtml

来源错误:

在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪:

[InvalidOperationException: The view 'Hi!!, I am Sagar' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Hi!!, I am Sagar.aspx
~/Views/Home/Hi!!, I am Sagar.ascx
~/Views/Shared/Hi!!, I am Sagar.aspx
~/Views/Shared/Hi!!, I am Sagar.ascx
~/Views/Home/Hi!!, I am Sagar.cshtml
~/Views/Home/Hi!!, I am Sagar.vbhtml
~/Views/Shared/Hi!!, I am Sagar.cshtml
~/Views/Shared/Hi!!, I am Sagar.vbhtml]
   System.Web.Mvc.ViewResult.FindView(ControllerContext context) +506
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +230
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +74
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +388
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +72
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +303
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +155
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +184
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +136
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +40
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +45
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629708
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

【问题讨论】:

  • 你想做什么?

标签: c# asp.net .net asp.net-mvc-4


【解决方案1】:

希望对你有帮助

 [HttpPost]
        public ActionResult   getValues(ModelClass mClass)
        {
            mClass.userName = "Hi!!, I am Sagar";
            return Content(mClass);
        }

返回模型,而不是字符串(mClass.userName)。

【讨论】:

  • 这是正确答案,需要将实际的 Class 传递回视图,因为这是 View 的模型所绑定的!
  • 这是最好的答案@Namo
  • 是的,因为您介意该文本框的确切模型属性。你应该先去asp.net网站学习一下。
  • 其实我没用过return Content,我用returnn view或者json
【解决方案2】:

你必须这样做:

采取行动:

public ActionResult Index()
{
ModelClass model = new ModelClass();
model.userName  = "Hi its get Action";
return View(model);
}

查看:

@model  KendoUIMvcApplication.Models.ModelClass

@using (Html.BeginForm("Index", "Home", FormMethod.Post, null))
    {
        @Html.TextBoxFor(model=>model.userName)

        <input type="submit" value="Save"/>
    }

发布操作:

[HttpPost]
public ActionResult Index(ModelClass model)
{
model.userName = "Hi!!, I am Sagar";
return View(model);
}

希望对您有所帮助。有什么困惑可以评论我的帖子。

【讨论】:

  • 先生,请在执行此操作后查看新错误...我在上面进行了编辑
  • 你确定你写的代码和我贴的一样
  • 不,先生,对不起,我没有写 return View(mClass); ...但现在我写了...但它没有将文本框的值设置为我们分配给用户名的任何文本
  • 应该可以,请更新您在帖子中使用更新标题尝试的代码
  • 在获取操作中执行此操作:return View(model);我忘了在视图中传递模型对象
【解决方案3】:

我不确定这是否是整个问题,但您想使用:

@Html.TextBoxFor(model=>model.userName)

而不是

@Html.TextBoxFor(model=>model.userName.ToString())

TextBoxFor 使用反射绑定到模型属性,因此调用 ToString 可以防止输入的值被发送回控制器。

我也怀疑你想要

return View(mClass);

而不是

return Content(mClass.userName);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    相关资源
    最近更新 更多