传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [System.String]

时间:2014-04-21 12:33:15

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

我是第一次使用MVC4进行编程。

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

我做了以下事情:

HomeController中:

 [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())

    }

ModelClass:

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:未找到视图'Hi !!,我是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

3 个答案:

答案 0 :(得分:3)

希望这有帮助

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

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

答案 1 :(得分:1)

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

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

而不是

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

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

我也怀疑你想要

return View(mClass);

而不是

return Content(mClass.userName);

答案 2 :(得分:1)

你必须这样做:

采取行动:

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);
}

希望它有所帮助。您可以对我的帖子发表任何疑惑。