简体   繁体   中英

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

I am first time programming with MVC4.

I have simple textbox and want to access it from Controller.

I done following:

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

Its giving me error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String]', but this dictionary requires a model item of type 'KendoUIMvcApplication.Models.ModelClass'.

New Error:

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.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

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[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

Hope this helps

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

return the model , instead of string(mClass.userName).

I'm not sure if it's the whole problem but you want to use:

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

instead of

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

TextBoxFor uses reflection to bind to the model property so calling ToString prevents the entered value from being sent back to the controller.

I also suspect you want

return View(mClass);

instead of

return Content(mClass.userName);

you have to do like this:

Get Action:

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

View:

@model  KendoUIMvcApplication.Models.ModelClass

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

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

post action:

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

Hope it helps. Any confusion you can comment on my post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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