Hola a todos desarrolle un crud sencillo con C# MVC 5 usando SQL, usando entity para la DB, siguiendo este tutorial https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started lo adapte para un proyecto agregue view bags para llenar un dropdownlist, pero me hizo quedar mal ya que en la primera ocasion que lo probo mi jefe y quiso registrar la información no funciono, y en visual ni siquiera estaba corriendo, lo cual se me hizo muy raro, me comentaron que quizaa por correrlo como debug sin administrador despues segui modificandolo, y dias despues cuando el lo volvio a probar marco un nuevo error There is no ViewData item of type 'IEnumerable' that has the key 'Estados1'. les dejo mi metodo create, y el dropdown y al final el error completo, por cierto lo he probado sin modificar nada y a mi me funciona perfecto no entiendo porque podria fallar solo en una ocasion, digo lo he probadom ya mil veces podria ser la computadora que es lenta? digo para llevar la mia, siento que ya casi estoy despedido jaja controller:
public ActionResult Create()
{
var municipios = db.Municipios.Select(c => new {
Id = c.ID,
municipios = c.Municipio,
estados = c.Estado
}).ToList();
ViewBag.municipios = new MultiSelectList(municipios, "municipios", "municipios");
ViewBag.estados = new MultiSelectList(municipios, "estados", "estados");
return View();
}
View:
@Html.LabelFor(model => model.LugarNacimiento,"Lugar de Nacimiento", htmlAttributes: new { @class = "control-label col-md-2" })
<input type="hidden" name="LugarNacimiento" id="LugarNacimiento" />
<div class="col-md-2">
@Html.DropDownList("Estados1", (MultiSelectList)ViewBag.estados, new { required = "required", @class = "estados1 js-example-basic-single" })
</div>
<div class="col-md-2">
@Html.DropDownList("LugarNacimientoSelect", (MultiSelectList)ViewBag.municipios, new { required = "required", @class = "js-example-basic-single" })
@Html.ValidationMessageFor(model => model.LugarNacimiento, "", new { @class = "text-danger" })
</div>
Error:
Process information:
Process ID: 1396
Process name: iisexpress.exe
Account name: INAABA11\inavant
Exception information:
Exception type: InvalidOperationException
Exception message: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Estados1'.
at System.Web.Mvc.Html.SelectExtensions.GetSelectData(HtmlHelper htmlHelper, String name)
at System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, ModelMetadata metadata, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes)
at System.Web.Mvc.Html.SelectExtensions.DropDownList(HtmlHelper htmlHelper, String name, IEnumerable`1 selectList, Object htmlAttributes)
at ASP._Page_Views_Candidatos_Create_cshtml.Execute() in C:\Users\inavant\Documents\RegistroCandidatos crud terminado 2 copy - Copy\RegistroCandidatos\Views\Candidatos\Create.cshtml:line 32
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.StartPage.RunPage()
at System.Web.WebPages.StartPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_6.<BeginInvokeAction>b__3()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_1.<BeginInvokeAction>b__5(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)