MVC.
ASP.NET MVC
- - , -, -. - W3C, .
- MVC ASP.NET Web Application Microsoft Visual Studio Express.
1
MVC :
1. -, HTTP , - .
2. , .
3. ( ).
4
MvcApplication - , . URL-, App_Start\RouteConfig.cs:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
HTTP GET URL- http://localhost:36803/Account/Register Account Register ( *Controller).
namespace WebUI.Areas.Admin.Controllers
{
[Authorize(Roles = "admin")]
public class PageTextAdminController: Controller
{
IGenericRepository<PageText> textRepository;
public PageTextAdminController(IGenericRepository<PageText> textRepository)
{ this.textRepository = textRepository; }
public ActionResult Index()
{
var items = textRepository.GetAll();
return View(items);
}
[HttpGet]
public ActionResult Create()
{
return RedirectToAction("Edit");
}
[HttpGet]
public ViewResult Edit(int? id)
{
ViewBag.ArticleId = id?? 0;
var article = (id == null)? new PageText(): textRepository.GetAll().Where(x => x.Id == id).FirstOrDefault();
return View(article);
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(PageText text)
{
if (ModelState.IsValid)
{
byte[] txtfile = null;
txtfile = ExtensionsCollection.GetFileContents("File0", Request);
if (text.Id == 0)
{
textRepository.Add(text);
textRepository.Save();
}
else
{
textRepository.Edit(text);
|
|
textRepository.Save();
}
if (Request.Files["FileLogo"]!= null && Request.Files["FileLogo"].ContentLength > 0)
{
byte[] image = new Byte[Request.Files["FileLogo"].ContentLength];
Request.Files["FileLogo"].InputStream.Read(image, 0, Request.Files["FileLogo"].ContentLength);
text.AddLogo(image);
}
TempData["Message"] = text.Name + " .";
return RedirectToAction("Index");
}
else
return View(text);
}
public ActionResult Delete(int id)
{
var article = textRepository.FindBy(x => x.Id == id).FirstOrDefault();
if (article!= null)
{
article.DeleteFiles();
textRepository.Delete(article);
textRepository.Save();
}
TempData["Message"] = " .";
return RedirectToAction("Index");
}
}
- . (Authorize, AllowAnonymous). HttpGet HttpPost, , . , HTML- Razor (C#- ):
model DomainModel.Model.Article
@{
ViewBag.Title = " ";
Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}
<h2> </h2>
<div>@Html.ValidationSummary()</div>
@using (Html.BeginForm("Edit", "ArticleAdmin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset>
<legend> </legend>
<ol>
<li>
@Html.Label(" ")
<input name="FileLogo" type="file" />
<img style="max-height:100px;" src="@Model.GetLogoUrl()" />
</li>
<li>
@Html.TextBoxFor(model => model.Posted)
<li>
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.Posted)
@Html.Label("")
@Html.TextBoxFor(model => model.Name)
</li>
<li>
@Html.LabelFor(x => x.MenuName)
@Html.TextBoxFor(model => model.MenuName)
</li>
<li>
</ol>
</fieldset>
<fieldset class="submit">
<input type="submit" value="" />
</fieldset>
}
<div>
@Html.ActionLink(" ", "Index")
</div>
@{........} C#. @model , Model.
@model IEnumerable<DomainModel.Model.Article>
@{
ViewBag.Title = "";
Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}
<h2>
</h2>
<table class="grid">
<tr>
<th>
</th>
|
|
<th>
Id
</th>
<th>
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.ActionLink(". ", "Edit", new { id = item.Id }) |
@Html.ActionLink(". ", "Delete", new { id = item.Id }, new { @class = "Delete" })
</td>
<td>
@Html.Encode(item.Id)
</td>
<td>
@Html.Encode(item.Name)
</td>
<td>
@Html.CheckBox("_", item.IsTarget, new {disabled="true"})
</td>
</tr>
}
</table>
<p>
@Html.ActionLink("", "Create")
</p>