ASP.NET MVC #12
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۱/۲۱ ۰۰:۲۰:۰۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
namespace MvcApplication8.Models
{
public class Employee
{
public int Id { set; get; }
public string Name { set; get; }
public decimal Salary { set; get; }
public string Address { set; get; }
public bool IsMale { set; get; }
public DateTime AddDate { set; get; }
}
}
using System;
using System.Web.Mvc;
using MvcApplication8.Models;
namespace MvcApplication8.Controllers
{
public class EmployeeController : Controller
{
public ActionResult Create()
{
var employee = new Employee { AddDate = DateTime.Now };
return View(employee);
}
}
}
@model MvcApplication8.Models.Employee
@{
ViewBag.Title = "Create";
}
<h2>Create An Employee</h2>
@using (Html.BeginForm(actionName: "Create", controllerName: "Employee"))
{
@Html.EditorForModel()
<input type="submit" value="Save" />
}
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace MvcApplication8.Models
{
public class Employee
{
//[ScaffoldColumn(false)]
[HiddenInput(DisplayValue=false)]
public int Id { set; get; }
public string Name { set; get; }
[DisplayName("Annual Salary ($)")]
public decimal Salary { set; get; }
public string Address { set; get; }
[DisplayName("Is Male?")]
public bool IsMale { set; get; }
[DisplayName("Start Date")]
[DataType(DataType.Date)]
public DateTime AddDate { set; get; }
}
}
[DataType(DataType.MultilineText)]
[DisplayName("Gender")]
[UIHint("GenderOptions")]
public bool IsMale { set; get; }
@model bool
<p>@Html.RadioButton("", false, !Model) Female</p>
<p>@Html.RadioButton("", true, Model) Male</p>
@model Enum
@Html.DropDownListFor(m => m, Enum.GetValues(Model.GetType())
.Cast<Enum>()
.Select(m => {
string enumVal = Enum.GetName(Model.GetType(), m);
return new SelectListItem() {
Selected = (Model.ToString() == enumVal),
Text = enumVal,
Value = enumVal
};
}))
[UIHint("Enum")]
[DisplayFormat(NullDisplayText = "-")]
[DisplayFormat(DataFormatString = "{0:n}")]
[DisplayFormat(DataFormatString = "{0:n}", ApplyFormatInEditMode = true)]
using System;
using System.Collections.Generic;
namespace MvcApplication8.Models
{
public class Employees
{
public IList<Employee> CreateEmployees()
{
return new[]
{
new Employee { Id = 1, AddDate = DateTime.Now.AddYears(-3), Name = "Emp-01", Salary = 3000},
new Employee { Id = 2, AddDate = DateTime.Now.AddYears(-2), Name = "Emp-02", Salary = 2000},
new Employee { Id = 3, AddDate = DateTime.Now.AddYears(-1), Name = "Emp-03", Salary = 1000}
};
}
}
}
public ActionResult EmployeeList()
{
var list = new Employees().CreateEmployees();
return View(list);
}
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsMale)
</td>
<td>
@Html.DisplayFor(modelItem => item.AddDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
\Views\Employee\_EmployeeItem.cshtml
@model MvcApplication8.Models.Employee
<tr>
<td>
@Html.DisplayFor(x => x.Name)
</td>
<td>
@Html.DisplayFor(x => x.Salary)
</td>
<td>
@Html.DisplayFor(x => x.Address)
</td>
<td>
@Html.DisplayFor(x => x.IsMale)
</td>
<td>
@Html.DisplayFor(x => x.AddDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
@Html.ActionLink("Details", "Details", new { id = Model.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = Model.Id })
</td>
</tr>
@foreach (var item in Model) {
@Html.Partial("_EmployeeItem", item)
}
using System.Web.Mvc;
namespace MvcApplication8.Controllers
{
public class MenuController : Controller
{
[ChildActionOnly]
public ActionResult ShowMenu(string options)
{
return PartialView(viewName: "_ShowMenu", model: options);
}
}
}
@model string
<ul>
<li>
@Model
</li>
</ul>
@Html.Action(actionName: "ShowMenu", controllerName: "Menu",
routeValues: new { options = "some data..." })
@{ Html.RenderAction("About"); }
And not @Html.RenderAction("About")
@using System.Globalization
@model Nullable<DateTime>
@helper ShamsiDateTime(DateTime info, string separator = "/", bool includeHourMinute = true)
{
int ym = info.Year;
int mm = info.Month;
int dm = info.Day;
var sss = new PersianCalendar();
int ys = sss.GetYear(new DateTime(ym, mm, dm, new GregorianCalendar()));
int ms = sss.GetMonth(new DateTime(ym, mm, dm, new GregorianCalendar()));
int ds = sss.GetDayOfMonth(new DateTime(ym, mm, dm, new GregorianCalendar()));
if (includeHourMinute)
{
@(ys + separator + ms.ToString("00") + separator + ds.ToString("00") + " " + info.Hour + ":" + info.Minute)
}
else
{
@(ys + separator + ms.ToString("00") + separator + ds.ToString("00"))
}
}
@if (@Model.HasValue)
{
@ShamsiDateTime(@Model.Value , separator: "/", includeHourMinute: false)
}
public ActionResult Index()
{
var query = db.Users.Select(c => new SelectListItem
{
Value = c.UserId,
Text = c.UserName,
Selected = c.UserId.Equals(3)
});
var model = new MyFormViewModel
{
List = query.ToList()
};
return View(model);
}
public class MyFormViewModel
{
public int UserId { get; set; }
public IList<SelectListItem> List { get; set; }
}
@model MyFormViewModel @Html.DropDownListFor(m => m.UserId, Model.List, "--Select One--")
@model bool
<p>@Html.RadioButton("", false, !Model) Female</p>
<p>@Html.RadioButton("", true, Model) Male</p>
@model Enum
@Html.DropDownListFor(m => m, Enum.GetValues(Model.GetType())
.Cast<Enum>()
.Select(m => {
string enumVal = Enum.GetName(Model.GetType(), m);
return new SelectListItem() {
Selected = (Model.ToString() == enumVal),
Text = enumVal,
Value = enumVal
};
}))
The model item passed into the dictionary is null
سلام؛ زمانی که در پروژه از [UIHint("GenderOptions")] استفاده میکنم به خوبی خروجی میگیرم اما زمانی که که Model من در DataModel و یک پروژه مجزا و در یک Dll دیگر است کار نمیکند.
سلام
ممنون از پاسختون و همچنین پروژه ای که Attach کردید.
پس از مقایسه و پیگیری متوجه شدم اگر GenderOptions در فولدر Shared\EditorTemplates باشد کار میکند اما من این UiHint و صرفا برای یک ویو خاص میخوام. از این رو اگز این UidHint و در هر مسیری به غیر از مسیر فوق قرار بدم شناسایی نمیشه. این مسئله مخصوصا به این شکل است و یا من درجایی اشتباه کردم.
یک سوال دیگه که برام پیش اومده این است که ویو من اتوماتیک و با استفاده از متد @Hml.Editorformodels
ساخته میشه اما در متد Post مدلی به ویو پاس ندادم و صرفا return view() زدم و در متد Get کنترل پارامتری از نوع مدل مورد نظر گرفتم. حالا سوال من این است که درسته که در ابتدای ویو با @model myProject.MyModel به صورت Strongly type تعریف کردم اما در صدا زدن ویو مدلی را ارسال نکردم اما ویو من از روی strongly type ساخته میشه ! و این ساخته شدن مشخص نیست به چه شکله چرا که متد سازنده کلاس (Constructor) را هم صدا نمیزند!
باز هم ممنون از پاسختون.
public enum MyGrade { A = 20, B =15, C =10, }
[UIHint("Enum")]
public MyGrade Grade { set; get; }public abstract class BaseViewModel
{
public string Name { get; set; }
}public class HomeViewModel : BaseViewModel
{
public int Data1 { set; get;}
// ...
}@model BaseViewModel
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Test</title>
</head>
<body>
<header>
Hello @Model.Name
</header>
<div>
@this.RenderBody()
</div>
</body>
</html>// در پایه مدلها
public abstract class BaseViewModel
{
public IList<Post> Posts { get; set; } // خاصیت عمومی که قرار است در فایل مستر قابل دسترسی باشد
}
// در اکشن متد
return View(model: new HomeViewModel { Posts = .... });
// در اینجا ویوومدل ارسالی از پایه مدلها مشتق میشود
public class HomeViewModel : BaseViewModel
میخوام لیست گروهبندی مطالبمو تو _Layout.cshtml ( نمایش در تمام صفحات مثل MasterPage در WebForm) نشون بدم چه راهی رو پیشنهاد میدید ؟ خیلی ممنون
[DisplayFormat(DataFormatString = "{0:n}", ApplyFormatInEditMode = true)]
public decimal cost { get; set; } @model MyMVCProject.Models.MyModel . . . . @Html.EditorFor(m => m.cost) @*@Html.TextBoxFor(m => m.cost)*@
<script src="../../Scripts/jquery-1.9.1.min.js" type="text/javascript"></script> <script src="../../Scripts/jquery.price_format.2.0.js" type="text/javascript"></script>
ولی باز هم نتیجه نگرفتم. میشه لطف کنید راهنمائیم کنید که چطور از اون مثال استفاده کنم؟
src='@Url.Content("~/Contents/Scripts/jQuery.js")' src='~/Contents/Scripts/jQuery.js'
Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.