繁体   English   中英

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

[英]The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String]

我是第一次使用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 / Hi !!,我是Sagar.aspx〜/ Views / Home / Hi !!,我是Sagar.ascx〜/ Views / Shared / Hi !!,我是Sagar。 aspx〜/ Views / Shared / Hi !!,我是Sagar.ascx〜/ Views / Home / Hi !!,我是Sagar.cshtml〜/ Views / Home / Hi !!,我是Sagar.vbhtml〜/ Views /共享/嗨!,我是Sagar.cshtml〜/ Views / Shared / Hi !!,我是Sagar.vbhtml

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

异常详细信息:System.InvalidOperationException:找不到“嗨!,我是Sagar”视图或其主视图,或者没有视图引擎支持搜索到的位置。 搜索了以下位置:〜/ Views / Home / Hi !!,我是Sagar.aspx〜/ Views / Home / Hi !!,我是Sagar.ascx〜/ Views / Shared / Hi !!,我是Sagar。 aspx〜/ Views / Shared / Hi !!,我是Sagar.ascx〜/ Views / Home / Hi !!,我是Sagar.cshtml〜/ Views / Home / Hi !!,我是Sagar.vbhtml〜/ Views /共享/嗨!,我是Sagar.cshtml〜/ Views / Shared / Hi !!,我是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

希望这可以帮助

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

返回模型,而不是string(mClass.userName)。

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

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

代替

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

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

我也怀疑你要

return View(mClass);

代替

return Content(mClass.userName);

您必须这样做:

采取行动:

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

希望能帮助到你。 您可以在我的帖子中发表任何困惑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM