Tôi đã duyệt web đang cố gắng tìm một ví dụ/hướng dẫn chi tiết về cách tạo và sử dụng Trình trợ giúp HTML tùy chỉnh của riêng tôi cho ứng dụng MVC 3 Razor của tôi như sauThêm một Trình trợ giúp HTML tùy chỉnh vào Dự án MVC
Adding your own HtmlHelper in ASP.NET MVC 3
tôi đã tạo ra một lớp (tỉa nó xuống một chút) như vậy
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace MyWebApp
{
public static class ExtensionMethods
{
public static MvcHtmlString StateDropDownListFor<TModel, TValue>
(this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression)
{
Dictionary<string, string> stateList = new Dictionary<string, string>()
{
{"AL"," Alabama"},
{"AK"," Alaska"},
{"AZ"," Arizona"},
{"AR"," Arkansas"}
};
return html.DropDownListFor(expression,
new SelectList(stateList, "key", "value"));
}
}
}
Cho đến nay rất tốt,
Bên trong bộ điều khiển của tôi, tôi cũng nói thêm tài liệu tham khảo
using System.Web.Mvc.Html;
Bây giờ bên trong quan điểm của tôi tôi đã sau
@Html.StateDropDownList(x => x.State)
Nhưng tôi nhận được lỗi sau
System.web.mvc.htmlhelper<system.collections.generic.list<Profile.ProfileImages>> does not contain a definition for StateDropDownList and no extension method StateDropDownList acception a first argument of type system.web.mvc.htmlhelper<System.Collections.Generic.List<Profile.ProfileImages>> could be found(Are you missing a using directive of an assembly reference?)
Ai đó có thể xin vui lòng cho tôi biết những gì im làm sai ở đây.
Bạn có trình trợ giúp HTML có tên DisplayImageFor không? –
@ UfukHacıoğulları Xin lỗi tôi đã thêm tuyên bố lỗi không chính xác, tôi đã cập nhật ở trên. –