我有一个非静态类Institute,如果对象没有传递给这个视图,我就不能使用静态属性DisplayPicturePath。但是,如果我使用静态方法的静态类,那么在视图中可用。如果我的方法有误,可以提出一些建议吗?
[HttpGet]
public ActionResult Create() {
//if do not pass institute into view
//Institute institute = new Institute();
return View();
}
我的剃刀视图有这个
<div class="ui segment">
<img id="DisplayPicture_img" alt="Institute Picture" class="ui fluid image" src="@Url.Content(Path.Combine(Institute.DisplayPicturePath ,"univeristy.jpg"))" />
</div>
我的课就是这个
public class Institute
{
public Institute()
{
DisplayPicture = "institute.png";
Scholarships = new List<Scholarship>();
ModifiedDate = DateTime.Now;
EntryDate = DateTime.Now;
}
[NotMapped]
public static string DisplayPicturePath
{
get
{
return @"\\"+ Path.Combine("Content", "images", "DisplayPictures");
}
}
[Key]
[DisplayName("Institute Id")]
public int InstituteId { get; set; }
[DisplayName("Institute Name")]
public string Name { get; set; }}
}
我怎么使用另一个静态类来使用这种方式
<div class="ui segment">
<img id="DisplayPicture_img" alt="intitute Picture" class="ui fluid image" src="@Url.Content(Path.Combine(UtilityMethods.GetDisplayPicturePath(PictureType.Institute),"univeristy.jpg"))" />
</div>
我的静态类是
public static class UtilityMethods
{
public static string GetDisplayPicturePath(PictureType type)
{
if (type.Equals(PictureType.Institute))
{
return @"\\" + Path.Combine("Content", "images", "DisplayPictures", "Institute");
}
else if(type.Equals(PictureType.User))
{
return @"\\" + Path.Combine("Content", "images", "DisplayPictures", "User");
}
else if (type.Equals(PictureType.Scholarship))
{
return @"\\" + Path.Combine("Content", "images", "DisplayPictures", "Scholarship");
}
else
{
return @"\\" + Path.Combine("Content", "images");
}
}
}
答案 0 :(得分:0)
这是一个Build Issue或Chrome浏览器缓存清除问题,因为Static属性在我的控制器中可用但由于某些原因在同一时间无法查看。
晚餐休息后,睡个好觉。我通过创建大量的测试场景来重新检查它,并且最终确定原始方法正在运行。
静态属性在视图中绝对可用。 天气它们是方法形式,Const字符串形式或从非静态类调用。
答案 1 :(得分:-2)
如果属性不是静态的,除非在模型
中传递,否则无法在视图中访问该属性`[HTTPGET]
public ActionResult Create() {
//if do not pass institute into view
Institute institute = new Institute();
institute.DisplayPicturePath=//Whatever value you wanna set here;
return View(institute);
}`
查看:@model Institute src =“@ Url.Content(Model.DisplayPicturePath)”/&gt;