- Details
- Written by: Stanko Milosev
- Category: MVC 3
- Hits: 5345
Writing HTML Helpers
page 330,
book Professional ASP.NET MVC 3
For example in: \MvcApplication1\MvcApplication1\Core\Extensions.cs write something like:
using System;
using System.IO;
using System.Web.Mvc;
namespace MvcApplication1.Core
{
public static class HtmlExtensions
{
private class Extensions : IDisposable
{
private readonly TextWriter _writer;
public Extensions(TextWriter writer)
{
_writer = writer;
}
public void Dispose()
{
_writer.Write("</table>");
}
} //private class Extensions : IDisposable
//note that this part is not under private class Extensions : IDisposable
public static IDisposable Begin(this HtmlHelper html)
{
var writer = html.ViewContext.Writer;
writer.Write("<table>");
return new Extensions(writer);
}
}
}
And in: \MvcApplication1\MvcApplication1\Views\Stanko\Index.cshtml Something like:
@using (Html.Begin())
{
}
after starting app in the HTML source I will see tag <table></table> from here
- Details
- Written by: Stanko Milosev
- Category: MVC 3
- Hits: 4845
In Global.asax.cs RegisterRoutes change to:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Default1", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
This mean that Default1 controller will be called, action Index (also view)... It is called from:
protected void Application_Start()
and Application_Start() is place where application begans
- Details
- Written by: Stanko Milosev
- Category: MVC 3
- Hits: 4920
If your model look something like:
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Infrastructure;
namespace MvcApplication3.Models
{
public class MyTable
{
[Key]
public int MyNum { get; set; }
public string Name { get; set; }
}
public class MyTableDBContext : DbContext
{
public DbSet MyTable { get; set; }
}
}
Then, your connection string in Web.config must look like:
<add name="MyTableDBContext" connectionString="Data Source=MyPC; Initial Catalog=MyDb; Integrated Security=False; Persist Security Info=True; User ID=myUser; Password=myPass" providerName="System.Data.SqlClient" />
Note that MyTableDBContext is in connection string and defined as DbContext in the model, and Integrated Security is false (true means to Windows Authentication)
- Details
- Written by: Stanko Milosev
- Category: MVC 3
- Hits: 6724
Inserting in non identity ID column:
Table:
CREATE TABLE [dbo].[TestTable]( [Id] [int] NOT NULL, [Name] [nvarchar](50) NULL, CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
Model:
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace NoIdent.Models
{
[Table("TestTable")]
public class TestTable
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public string Name { get; set; }
}
public class testNoIdentEntities : DbContext
{
public DbSet TestTable { get; set; }
}
}
Without [Table("TestTable")], EF will create TestTableS - EF always add S (plural)
[DatabaseGenerated(DatabaseGeneratedOption.None)] is mandatory, to tell EF that ID is not identity
Create controller with scaffolding. Modify Create and Index view:
Create:
<fieldset> <legend>TestTable</legend> <div class="editor-label"> @Html.LabelFor(model => model.Id) </div> <div class="editor-field"> @Html.EditorFor(model => model.Id) @Html.ValidationMessageFor(model => model.Id) </div> <div class="editor-label"> @Html.LabelFor(model => model.Name) </div> <div class="editor-field"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> <p> <input type="submit" value="Create" /> </p> </fieldset>
Index:
<table>
<tr>
<th>
Id
</th>
<th>
Name
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</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>
}
</table>
and that's all folks!