Showing posts with label ASP.NET MVC. Show all posts
Showing posts with label ASP.NET MVC. Show all posts
Sunday, May 10, 2015
Sunday, May 3, 2015
button to Actionlink
http://www.codeproject.com/Tips/198477/Calling-a-MVC-Controller-and-Action-Method-using-H
Tuesday, April 28, 2015
Angular UI and asp.net mvc
http://www.codeproject.com/Articles/806500/Getting-started-with-AngularJS-and-ASP-NET-MVC-P
Sunday, April 26, 2015
radio button
http://stackoverflow.com/questions/17990310/how-to-align-inline-radios-checkboxes-and-help-block-elements-in-horizontal-fo
@model RegMSW.Web.Models.flightInfo
@using (Html.BeginForm())
{
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.pickup, htmlAttributes: new { @style = "width: 200px", @class = "control-label col-md-2" })
<div class="col-md-8">
<div id="flightInfoTemplate" >
<label class="radio-inline">
@Html.RadioButtonFor(model => model.pickup, "pickup") pickup
</label>
<label class="radio-inline">
@Html.RadioButtonFor(model => model.pickup, "dropoff") dropoff
</label>
<label class="radio-inline">
@Html.RadioButtonFor(model => model.pickup, "none", new { @checked = "checked" }) none
</label>
@Html.ValidationMessageFor(model => model.pickup, "", new { @class = "text-danger" })
</div>
@model RegMSW.Web.Models.flightInfo
@using (Html.BeginForm())
{
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.pickup, htmlAttributes: new { @style = "width: 200px", @class = "control-label col-md-2" })
<div class="col-md-8">
<div id="flightInfoTemplate" >
<label class="radio-inline">
@Html.RadioButtonFor(model => model.pickup, "pickup") pickup
</label>
<label class="radio-inline">
@Html.RadioButtonFor(model => model.pickup, "dropoff") dropoff
</label>
<label class="radio-inline">
@Html.RadioButtonFor(model => model.pickup, "none", new { @checked = "checked" }) none
</label>
@Html.ValidationMessageFor(model => model.pickup, "", new { @class = "text-danger" })
</div>
Thursday, April 23, 2015
Asp.net mvc persist data
http://stackoverflow.com/questions/15336991/what-is-correct-way-of-storing-data-between-requests-in-asp-mvc
list in a model
@model MvcApplication5.Models.EmployeeModelClass
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm())
{
<div>
Emp Name @Html.TextBox("Name",Model.Name) <br/><br/>
Address @Html.TextBox("Address",Model.Address) <br/><br/>
Contact @Html.TextBox("Contact",Model.Contact) <br/><br/>
@{
for (int i = 0; i < @ViewBag.totalDays; i++)
{
<span>@Html.DisplayFor(m => m.DeptList[i].OneDay, new { disabled = "disabled" })</span>
<span>@Html.EditorFor(m => m.DeptList[i].AttendThisDay)</span>
ViewBag.i = Model.DeptList[i].OneDay;
}
}
<div>
<input type="submit" value="Submit" /> <br /><br />
</div>
@{
if (Model.Name != null)
{
<div>
Employee Name : @Model.Name <br/>
Address : @Model.Address <br/>
Contact : @Model.Contact <br/>
@{
if (Model.DeptList != null)
{
<table>
<tr>
<th>Dept code</th>
<th>Dept Name</th>
</tr>
@{
foreach (var dept in Model.DeptList)
{
<tr>
<td>@dept.OneDay</td>
<td>@dept.AttendThisDay </td>
</tr>
}
}
</table>
}
}
</div>
}
}
</div>
}
</body>
</html>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm())
{
<div>
Emp Name @Html.TextBox("Name",Model.Name) <br/><br/>
Address @Html.TextBox("Address",Model.Address) <br/><br/>
Contact @Html.TextBox("Contact",Model.Contact) <br/><br/>
@{
for (int i = 0; i < @ViewBag.totalDays; i++)
{
<span>@Html.DisplayFor(m => m.DeptList[i].OneDay, new { disabled = "disabled" })</span>
<span>@Html.EditorFor(m => m.DeptList[i].AttendThisDay)</span>
ViewBag.i = Model.DeptList[i].OneDay;
}
}
<div>
<input type="submit" value="Submit" /> <br /><br />
</div>
@{
if (Model.Name != null)
{
<div>
Employee Name : @Model.Name <br/>
Address : @Model.Address <br/>
Contact : @Model.Contact <br/>
@{
if (Model.DeptList != null)
{
<table>
<tr>
<th>Dept code</th>
<th>Dept Name</th>
</tr>
@{
foreach (var dept in Model.DeptList)
{
<tr>
<td>@dept.OneDay</td>
<td>@dept.AttendThisDay </td>
</tr>
}
}
</table>
}
}
</div>
}
}
</div>
}
</body>
</html>
Tuesday, April 21, 2015
Web config remote only
Custom error on
Filters. Add handle error attribute
checkbox toggle only work once
http://stackoverflow.com/questions/29779113/asp-net-mvc-checkbox-jquery-toggle-only-work-once
if do not use Html.IdFor the it would not work.
<script type="text/javascript">
$(function() {
$('#@(Html.IdFor(model => model.IsActive ))').change(function() {
$('#ShowHideMe').toggle($(this).is(':checked'));
});
});
</script>
using System;
using System.Linq;
namespace checkbox.Models
{
public class Person
{
public bool IsActive { get; set; }
public string Email { get; set; }
}
}
@model checkbox.Models.Person
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div id="myCheckbox" class="form-group">
@Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsActive)
@Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div id="ShowHideMe" class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
if do not use Html.IdFor the it would not work.
<script type="text/javascript">
$(function() {
$('#@(Html.IdFor(model => model.IsActive ))').change(function() {
$('#ShowHideMe').toggle($(this).is(':checked'));
});
});
</script>
using System;
using System.Linq;
namespace checkbox.Models
{
public class Person
{
public bool IsActive { get; set; }
public string Email { get; set; }
}
}
@model checkbox.Models.Person
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div id="myCheckbox" class="form-group">
@Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsActive)
@Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div id="ShowHideMe" class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
Monday, April 20, 2015
mvc model has a list
http://stackoverflow.com/questions/25092497/c-sharp-mvc-list-and-create-in-same-view
http://www.codeproject.com/Tips/855577/List-of-Model-Object-Post-to-Controller-in-ASP-NET
hat is so mach pretty easier
and controller
and model
http://www.codeproject.com/Tips/855577/List-of-Model-Object-Post-to-Controller-in-ASP-NET
hat is so mach pretty easier
Hide Copy Code
@using (Html.BeginForm())
{
@Html.DisplayFor(model => model.Step)
for (var i = 0 ; i< Model.List.Count; i++)
{
<span>@Html.TextBoxFor(m => m.List[i].ID) </span> <span>@Html.TextBoxFor(m => m.List[i].Name) </span>
}
<input type="submit" value="Submit" />
}
and controller
Hide Copy Code
public ActionResult Index()
{
var model = new MyModel(){Step = 1, List = new List<MyModelEnum>()};
model.List.Add(new MyModelEnum() { ID = 1, Name = "1"});
model.List.Add(new MyModelEnum() { ID = 2, Name = "2" });
model.List.Add(new MyModelEnum() { ID = 3, Name = "3" });
model.List.Add(new MyModelEnum() { ID = 4, Name = "4" });
model.List.Add(new MyModelEnum() { ID = 5, Name = "5" });
return View(model);
}
[HttpPost]
public ActionResult Index(MyModel model)
{
return View(model);
}
and model
Hide Copy Code
public class MyModel
{
public int Step { get; set; }
public List<MyModelEnum> List { get; set; }
}
public class MyModelEnum
{
public int ID { get; set; }
public string Name { get; set; }
}
Sunday, April 19, 2015
Partial view injection
https://evolpin.wordpress.com/2011/04/12/asp-net-mvc-partialview-with-ajax/
https://evolpin.wordpress.com/2011/04/26/asp-net-mvc-partial-view-and-ajax-real-world-example/
Basically there are 4 steps to achieve this:
- Create a PartialView.
- Create a place holder html control.
- Use jQuery’s load method to fetch the partial view from the server and inject it into the place holder.
- Create a server side Action in a Controller that will return the partial view.
Here’s a quick example how this can be done. We’ll use the same partial view created earlier (i.e. MyPartialView.cshtml). So now we have to prepare a place holder div, and use jQuery to load and inject the response:
1: <a href='javascript:getView();'>Get Partial View</a>
2: <script type="text/javascript">
3: function getView() {
4: $('#divResult').load("@Url.Action("GetView" , "Home" )");
5: }
6: </script>
7: <div id='divResult'>
8: </div>
- Line 1 is a simple anchor which will invoke the JavaScript containing jQuery’s load.
- Line 4 is the simple one-line code which performs an ajax call to the server’s GetView action in the Home Controller (will be done shortly), and injects the result.
- Line 7-8 is where the result will be injected to.
And now for the server side – pretty self-explanatory:
1: public ActionResult GetView()
2: {
3: return PartialView("MyPartialView");
4: }
When we run the sample, prior to the Ajax call this looks like this:
Saturday, April 18, 2015
cookie
http://www.nullskull.com/a/810/aspnet-cookies-faq.aspx
http://www.codeproject.com/Articles/244904/Cookies-in-ASP-NET
http://www.code-sample.com/2014/08/aspnet-mvc-cookie-implementation.html
http://stackoverflow.com/questions/3716710/how-to-check-cookie-enabled-for-the-browser-or-not
HttpCookie mswEventIDCookie = new HttpCookie("mswEventIDCookie", id);
mswEventIDCookie.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(mswEventIDCookie);
if (Request.Cookies["mswEventIDCookie"] != null)
{
id = Request.Cookies["mswEventIDCookie"].Value;
}
MSWEvent mswEvent = repository.FindMSWEvent(idnew);
ViewBag.MSWEventName = mswEvent.Name;
ViewBag.MSWEventId = mswEvent._id;
Session
http://stackoverflow.com/questions/14138872/how-to-use-sessions-in-an-asp-net-mvc-4-application
asp.net mvc project index view not found when start debugging
I have to clean up what is in Specific page
http://stackoverflow.com/questions/1333002/how-do-you-set-the-startup-page-for-debugging-in-an-asp-net-mvc-application
Go to your project's properties and set the start page property.
- Go to the project's Properties
- Go to the Web tab
- Select the Specific Page radio button
- Type in the desired url in the Specific Page text box
Thursday, April 16, 2015
temp data
http://knowledgecaves.blogspot.com/2013/09/how-to-persist-value-from-one-request.html
http://www.dotnetcurry.com/showarticle.aspx?ID=1074
Thursday, April 9, 2015
MVC wizard nuget
https://www.nuget.org/packages/MVC.Wizard
[Serializable]
http://www.codeproject.com/Articles/124541/Working-on-JSON-objects-in-jQuery-and-MVC
http://stackoverflow.com/questions/25828553/cannot-find-html-serialize-helper-in-mvc-5-futures
below is better:
http://stackoverflow.com/questions/6148652/problem-with-the-mvcserializer-after-updating-from-mvc2-to-mvc3
http://www.codeproject.com/Articles/36781/Serialization-and-Deserialization-in-ASP-NET-with
http://stackoverflow.com/questions/25828553/cannot-find-html-serialize-helper-in-mvc-5-futures
below is better:
http://stackoverflow.com/questions/6148652/problem-with-the-mvcserializer-after-updating-from-mvc2-to-mvc3
http://www.codeproject.com/Articles/36781/Serialization-and-Deserialization-in-ASP-NET-with
Wednesday, April 8, 2015
Multiple partial view
http://www.codeproject.com/Tips/712187/Returning-More-Views-in-an-ASP-NET-MVC-Action
public ActionResult Sample()
{
MyClass cla = new MyClass { Student = new Student { ID = 1, Name = "Student" }, Teacher = new Teacher { ID=1, Name="Teacher" } };
return Json(cla,JsonRequestBehavior.AllowGet);
}
@Ajax.ActionLink("Click Me", "Sample", new AjaxOptions { HttpMethod="Get", OnComplete="_Complete"}) <script type="text/javascript"> function _Complete(data) { var JsonObj = $.parseJSON(data.responseText); $("#span1").html(JsonObj.Student.Name); $("#span2").html(JsonObj.Teacher.Name); } </script> <br /> <span id="span1"></span> <br /> <span id="span2"></span>
http://stackoverflow.com/questions/6342951/how-to-refresh-multiple-partialview-on-click-of-ajax-actionlink
Keep it simple - use jQuery - then you have total control:
$(function() {
$('#somelink').click(function(e) {
e.preventDefault();
$.get('/controller/action1', function(data) {
$('#up').html(data);
});
$.get('/controller/action2', function(data) {
$('#down').html(data);
});
});
});
However, since you're updating both panels, I would suggest wrapping those two middle panels in a partial view of it's own - then serve that via a single action method - that way you only need 1 ajax call.
Edit
As @FelixM mentions, you should use Url.Action or Url.RouteUrl to generate the URL for the AJAX call, so if your routes change then your JS need not, e.g:
.get('@Url.Action('Controller', 'Action1')', function(data)
or
.get('@Url.RouteUrl('SomeNamedRoute')', function(data)
If your putting this script in an external file then you'll need to use a technique to set the url in the main view, then read from the external variable.
Such techniques include a JavaScript variable, hidden field, passing URL to function as parameter, etc.
Tuesday, April 7, 2015
@html.actionlink viewbag
http://stackoverflow.com/questions/21026130/html-actionlink-value-of-viewbag
Monday, April 6, 2015
Form step by step navigation
http://www.c-sharpcorner.com/UploadFile/4b0136/getting-started-with-wizard-in-Asp-Net-mvc-5-part-1/
http://www.binaryintellect.net/articles/9a5fe277-6e7e-43e5-8408-a28ff5be7801.aspx
http://www.binaryintellect.net/articles/9a5fe277-6e7e-43e5-8408-a28ff5be7801.aspx
http://stackoverflow.com/questions/5923326/asp-net-mvc-web-application-html-displayfor-and-html-hiddenfor-give-different-r
http://www.highoncoding.com/Articles/647_Creating_Wizard_Using_ASP_NET_MVC_Part_1.aspx
http://afana.me/post/create-wizard-in-aspnet-mvc-3.aspx
below is the pdf version
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=0CDQQFjAD&url=http%3A%2F%2Fenterprise-nci.googlecode.com%2Ffiles%2FPro%2520ASP.NET%2520MVC%25202%2520Framework%2C%2520Second%2520Edition.pdf&ei=cU8mVfn2L8aosAWn5oAI&usg=AFQjCNFA5x3lQ954DbA5cO-Z7isTd7bPEA&sig2=qTq8c4dbd1Nsbb6lzx-vYQ&bvm=bv.90237346,d.b2w
Pro ASP.NET MVC 2 Framework
chapter 13 source code
http://www.apress.com/9781430228868?gtmf=sbelow is the pdf version
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=0CDQQFjAD&url=http%3A%2F%2Fenterprise-nci.googlecode.com%2Ffiles%2FPro%2520ASP.NET%2520MVC%25202%2520Framework%2C%2520Second%2520Edition.pdf&ei=cU8mVfn2L8aosAWn5oAI&usg=AFQjCNFA5x3lQ954DbA5cO-Z7isTd7bPEA&sig2=qTq8c4dbd1Nsbb6lzx-vYQ&bvm=bv.90237346,d.b2w
Sunday, April 5, 2015
Pass data to view and make it read only
http://stackoverflow.com/questions/20657706/mvc-4-html-hiddenfor-are-not-updating-on-a-postback
*** below is still not right, needs more work
in the controller create
string id is passed in to be the key to retrieve the event id
then pass to registration model (create a new mode), so registration can contain the event name and id. but I need to make even name can not be modified on the create view for registration, so make it readonly...
// GET: Registration/Create
public ActionResult Create(string id)
{
MSWEvent mswEvent = new MSWEvent();
mswEvent = repository.FindMSWEvent(new MongoDB.Bson.ObjectId(id));
string mswEventName = mswEvent.Name;
//ViewBag.MSWEventsList = mswEvents = GetMSWEventslist();
ViewBag.mswEventName = mswEventName;
Registration reg = new Registration();
reg.MSWEventName = mswEventName;
return View(reg);
}
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.MSWEventName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.MSWEventName, new {@disabled = "disabled", @readonly = "readonly", htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MSWEventName, "", new { @class = "text-danger" })
</div>
</div>

*** below is still not right, needs more work
in the controller create
string id is passed in to be the key to retrieve the event id
then pass to registration model (create a new mode), so registration can contain the event name and id. but I need to make even name can not be modified on the create view for registration, so make it readonly...
// GET: Registration/Create
public ActionResult Create(string id)
{
MSWEvent mswEvent = new MSWEvent();
mswEvent = repository.FindMSWEvent(new MongoDB.Bson.ObjectId(id));
string mswEventName = mswEvent.Name;
//ViewBag.MSWEventsList = mswEvents = GetMSWEventslist();
ViewBag.mswEventName = mswEventName;
Registration reg = new Registration();
reg.MSWEventName = mswEventName;
return View(reg);
}
in the create view
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.MSWEventName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.MSWEventName, new {@disabled = "disabled", @readonly = "readonly", htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MSWEventName, "", new { @class = "text-danger" })
</div>
</div>
Subscribe to:
Posts (Atom)