I have a model for a drop down menu similar to this one. I am trying to create a controller for it.
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View(new MyData());
}
And I create my View like this
#Html.DropDownListFor(m => m.State,
new SelectList(Model.StateList, "Value", "Text"))
But I am receiving the error below for the line above:
Compiler Error Message: CS1061: 'System.Collections.Generic.List<Projects.Models.MyData>' does not contain a definition for 'StateList' and no extension method 'StateList' accepting a first argument of type 'System.Collections.Generic.List<Projects.Models.MyData>' could be found (are you missing a using directive or an assembly reference?)
Can you please direct me what I am doing wrong?
the problem is #model List<ProjectName.Models.MyData>
simply change it to
#model Projects.Models.MyData
cheers!
Related
I use a child action in Home Controller like this:
[OutputCache(Duration=30)]
public ActionResult ChildAction()
{
Response.Write("Date Time is="+DateTime.Now);
return View();
}
Home.cshtml
<h2>About</h2>
<p>
#Html.Action("ChildAction")
</p>
but after run my asp.net mvc3.0 Project i get an error :
The view 'ChildAction' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/ChildAction.aspx
~/Views/Home/ChildAction.ascx
~/Views/Shared/ChildAction.aspx
~/Views/Shared/ChildAction.ascx
~/Views/Home/ChildAction.cshtml
~/Views/Home/ChildAction.vbhtml
~/Views/Shared/ChildAction.cshtml
~/Views/Shared/ChildAction.vbhtml
please help me how can i solve it
thanks
You should have a view named "ChildAction", or in action you should return view as
return View("ViewName"); In summary, view name should be same as action name, not controller name as you did(You named view as "Home").
I would like to use custom #Html.ActionLink
I am trying to use the following code:-
public static class LinkExtensions
{
public static MvcHtmlString MyActionLink(
this HtmlHelper htmlHelper,
string linkText,
string action,
string controller)
{
var currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
var currentController = mlHelper.ViewContext.RouteData.GetRequiredString("controller");
if (action == currentAction && controller == currentController)
{
var anchor = new TagBuilder("a");
anchor.Attributes["href"] = "#";
anchor.AddCssClass("currentPageCSS");
anchor.SetInnerText(linkText);
return MvcHtmlString.Create(anchor.ToString());
}
return htmlHelper.ActionLink(linkText, action, controller);
}
}
From Custom ActionLink helper that knows what page you're on
But I am getting
System.Web.Mvc.HtmlHelper' does not contain a definition for
'ActionLink' and no extension method 'ActionLink' accepting a first
argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you
missing a using directive or an assembly reference?
Add this using System.Web.Mvc.Html; on top of your file
Make sure you have the namespace for your extensions class included in your web.config. For example:
namespace MyProject.Extensions
{
public static class LinkExtensions
{
//code
}
}
In your site Web.config and/or Web.config located in your "Views" folder:
<system.web>
<pages>
<namespaces>
<add namespace="MyProject.Extensions" />
</namespaces>
</pages>
</system.web>
Otherwise include a "using" block for the namespace at the top of your view page can work but for common namespaces I would do the above.
ASPX:
<%# Import namespace="MyProject.Extensions" %>
RAZOR:
#using MyProject.Extensions
Don't forget that the first parameter only accepts string. It will show you this error if it's NOT.
Make sure that you have following using in your class file:
using System.Web.Mvc.Html;
This is needed because the HtmlHelper class is located in System.Web.Mvc namespace but the ActionLink extension method is located in System.Web.Mvc.Html namespace.
If your using nopcommerce add this using statement at the top of your view file.
#using Nop.Web.Framework.UI
My issue was, I had incomplete syntax, "#Html.actionLink", in a view. Looks like I had started to add an action link and went a different direction but forgot to remove the partial action link, this caused the same error as above.... So check your syntax as that will throw the same runtime error. Good luck!
I'm working on a MVC3 web application. I want a list of rotation shown in view. But during build I get error:
Error 2971 'System.Web.Mvc.SelectList' does not contain a definition
for 'MakeSelection' and no extension method 'MakeSelection' accepting
a first argument of type 'System.Web.Mvc.SelectList' could be found
(are you missing a using directive or an assembly reference?).
My code in view:
<div class="editor-field">
#Html.DropDownListFor(model => model.JobFiles[i].JobPages[j].UserRotation, (ViewData["rotation"] as SelectList).MakeSelection(Model.JobFiles[i].JobPages[j].UserRotation))
</div>
Please help. Thx in advance.
You error is telling you that the MakeSelection function does not belong to the System.Web.Mvc.SelectList object. I did find this extension method -
public static SelectList MakeSelection(this SelectList list, object selection)
{
return new SelectList(list.Items, list.DataValueField, list.DataTextField, selection);
}
in this question, are you missing the extension method from your code?
I created a custom Model i.e. to support my Razor View. Then I created a controller as following`namespace MyCandidate.Controllers
public class CandidateViewModelController : Controller
{
//
// GET: /CandidateViewModel/
public ActionResult Index()
{
return View();
}
}
I also have the following statement in my _Layout.cshtml
#Html.ActionLink("Canid", "Index", "CandidateViewModel")
Next i created a view and the very first statement of the view is
#model MyCandidate.Models.CandidateViewModel
when i run my project i get the following error
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
I spent more than 3 hours but could not figure out?
your Index() not get any parameters, but you send "CandidateViewModel") add Index(string input) method with attribute [HttpGet] to controller.
this error meen you haven't view "Index" in "Views/CandidateViewModel/Index.cshtml".
Maybe you delete master page files (_ViewSrat, _Layout)
Or you made a mistake when you change your routes
Change your ActionLink to:
#Html.ActionLink("Canid", "Index")
If you want to pass any data to View , you can also use ViewBag:
// Controller :
ViewBag.CandidateValues = CandidateViewModelData;
// View
#Html.Label("LabelName", (CandidateViewModel) ViewBag.CandidateValues.FiledName);
I have a situation where I want to use a custom EditorTemplate with a ViewModel. So I have my ViewModel...
class Aspect {
}
class AspectViewModel {
}
then my EditorTemplate
Views
Shared
EditorTemplates
Aspect.cshtml
Aspect.cshtml
#model AspectViewModel
// other html
Then in another view that takes AspectViewModel, I call #Html.EditorFor(model => model), but it does not work. It only works if I use a hard-coded string #Html.EditorForModel("Aspect").
Any idea why it isn't being called?
You should name the editor template AspectViewModel.cshtml if it is strongly typed to AspectViewModel. Then all you have to do is:
#model AspectViewModel
#Html.EditorForModel()
or
#model SomeViewModel
#Html.EditorFor(x => x.Aspect)
where the Aspect property of SomeViewModel is of type AspectViewModel.
The convention is that the editor/display should be named as the type of the property you are calling it on and not the name of this property.
They also work greatly with collections. For example if you have the following property:
public class SomeViewModel
{
public IEnumerable<AspectViewModel> Aspects { get; set; }
}
and you use:
#model SomeViewModel
#Html.EditorFor(x => x.Aspects)
then the ~/Views/Shared/EditorTemplates/AspectViewModel.cshtml editor template wil be rendered for each element of this collection. Thanks to this you really no longer need to ever write for/foreach loops in your views.
This is because your model is AspectViewModel, but your view name is Aspect. They must match exactly.