ASP.NET MVC3 view displays validation error if get request is invoked. How to show validation error only if post request is used like in MVC2 ?
View:
<%= Html.ValidationSummary(false, "Error") %>
Controller:
namespace Store.Controllers
{
public class CheckoutController : MyApp.Controllers.ControllerBase
{
[HttpGet]
public ActionResult Address()
{
return View(new AddressViewModel());
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Address(AddressViewModel model, string name) {
if (!ModelState.IsValid)
return View(model);
return RedirectToAction("Payment");
}
}
}
Related
I have a problem from my Asp.net MVC Blog website project.
I am filling textbox on admin website from this picture
but website any operation so its can't operation on database.
I sent you project code about my website project error.
public class AdminMakaleController : Controller
{
blogDB db = new blogDB();
// GET: AdminMakale
public ActionResult Index()
{
var makales = db.Makales.ToList();
return View(makales);
}
// GET: AdminMakale/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: AdminMakale/Create
public ActionResult Create()
{
ViewBag.KategoriId = new SelectList(db.Kategoris, "KategoriId", "KategoriAdi");
return View();
}
// POST: AdminMakale/Create
[HttpPost]
public ActionResult Create(Makale makale,string etiketler,HttpPostedFileBase Foto)
{
try
{
db.Makales.Add(makale);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
In ASP.NET MVC, I have recently found out that:
This doesn't work and results in an HTTP 404.
public class TestController : Controller
{
[HttpGet]
[HttpPost]
public ActionResult Index(TestModel model)
{
return View(model);
}
}
This works fine.
public class TestController : Controller
{
public ActionResult Index(TestModel model)
{
return View(model);
}
}
This also works fine:
public class TestController : Controller
{
[HttpGet]
[ActionName("Index")]
public ActionResult GetIndex(TestModel model)
{
return View("Index", model);
}
[HttpPost]
[ActionName("Index")]
public ActionResult PostIndex(TestModel model)
{
return View("Index", model);
}
}
I would like an explanation of why the first version doesn't work, but the other two do work. I would also appreciate if someone could tell me how I can modify the first version to make it work. I like the first version because it is more concise (1 method rather than 2) and also filters out unnecessary HTTP methods.
HTTP verb attributes are mutually exclusive, a request cannot be both GET and POST at the same time. Instead, you have to do this:
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult Index(TestModel model) { ... }
My question is about binding in MVC. Could you please help me with it?
My controller:
public class TestController : Controller
{
//
// GET: /Test/
[HttpGet]
public ActionResult Index()
{
return View(new TestModel());
}
public ActionResult Index(TestModel test)
{
return View(test);
}
}
My View:
#model MvcApplication1.Models.TestModel
#using (Html.BeginForm())
{
#Html.TextBoxFor(x => x.test) // or x.Test
<input type="submit" value="Set"/>
}
My model:
public class TestModel
{
public string test { get; set; } // or Test{get;set;}
}
The problem is connected with the name of parameter "test" in controller as I understand. I have just changed it to "model" and the binding is working. But it is not working in original state (the name of parameter is 'test'), 'test' parameter is null.
Please give me understand why the binding is not working in current example. Thank you a lot!
You need an [HttpPost] attribute on your second method. You also cannot use test as your variable name, as it is the same name as the property of the class you're attempting to bind to; the ModelBinder fails to determine which to use. Your code would look as follows:
public class TestController : Controller
{
//
// GET: /Test/
[HttpGet]
public ActionResult Index()
{
return View(new TestModel());
}
//
// POST: /Test/
[HttpPost]
public ActionResult Index(TestModel testModel)
{
return View(testModel);
}
}
I am redirecting the view from [HttpPost] method to [HttpGet] method. I have gotten it to work, but want to know if this is the best way to do this.
Here is my code:
[HttpPost]
public ActionResult SubmitStudent()
{
StudentViewModel model = TempData["model"] as StudentResponseViewModel;
TempData["id"] = model.Id;
TempData["name"] = model.Name;
return RedirectToAction("DisplayStudent");
}
[HttpGet]
public ActionResult DisplayStudent()
{
ViewData["id"] = TempData["id"];
ViewData["name"] = TempData["name"];
return View();
}
View:
<%# Page
Language="C#"
Inherits="System.Web.Mvc.ViewPage"
%>
<html>
<head runat="server">
<title>DisplayStudent</title>
</head>
<body>
<div>
<%= ViewData["id"]%> <br />
<%= ViewData["name"]%>
</div>
</body>
</html>
There are basically 3 techniques in ASP.NET MVC to implement the PRG pattern.
TempData
Using TempData is indeed one way of passing information for a single redirect. The drawback I see with this approach is that if the user hits F5 on the final redirected page he will no longer be able to fetch the data as it will be removed from TempData for subsequent requests:
[HttpPost]
public ActionResult SubmitStudent(StudentResponseViewModel model)
{
if (!ModelState.IsValid)
{
// The user did some mistakes when filling the form => redisplay it
return View(model);
}
// TODO: the model is valid => do some processing on it
TempData["model"] = model;
return RedirectToAction("DisplayStudent");
}
[HttpGet]
public ActionResult DisplayStudent()
{
var model = TempData["model"] as StudentResponseViewModel;
return View(model);
}
Query string parameters
Another approach if you don't have many data to send is to send them as query string parameters, like this:
[HttpPost]
public ActionResult SubmitStudent(StudentResponseViewModel model)
{
if (!ModelState.IsValid)
{
// The user did some mistakes when filling the form => redisplay it
return View(model);
}
// TODO: the model is valid => do some processing on it
// redirect by passing the properties of the model as query string parameters
return RedirectToAction("DisplayStudent", new
{
Id = model.Id,
Name = model.Name
});
}
[HttpGet]
public ActionResult DisplayStudent(StudentResponseViewModel model)
{
return View(model);
}
Persistence
Yet another approach and IMHO the best consists into persisting this model into some data store (like a database or something and then when you want to redirect to the GET action send only an id allowing for it to fetch the model from wherever you persisted it). Here's the pattern:
[HttpPost]
public ActionResult SubmitStudent(StudentResponseViewModel model)
{
if (!ModelState.IsValid)
{
// The user did some mistakes when filling the form => redisplay it
return View(model);
}
// TODO: the model is valid => do some processing on it
// persist the model
int id = PersistTheModel(model);
// redirect by passing the properties of the model as query string parameters
return RedirectToAction("DisplayStudent", new { Id = id });
}
[HttpGet]
public ActionResult DisplayStudent(int id)
{
StudentResponseViewModel model = FetchTheModelFromSomewhere(id);
return View(model);
}
Each method has its pros and cons. Up to you to choose which one suits best to your scenario.
If you are inserting this data into a database then you should redirect them to a controller action that has this data in the route:
/Students/View/1
You can then write code in the controller to retrieve the data back from the database for display:
public ActionResult View(int id) {
// retrieve from the database
// create your view model
return View(model);
}
One of the overrides of RedirectToAction() looks like that:
RedirectToAction(string actionName, object routeValues)
You can use this one as:
[HttpPost]
public ActionResult SubmitStudent()
{
StudentViewModel model = TempData["model"] as StudentResponseViewModel;
return RedirectToAction("DisplayStudent", new {id = model.ID, name = model.Name});
}
[HttpGet]
public ActionResult DisplayStudent(string id, string name)
{
ViewData["id"] = TempData["id"];
ViewData["name"] = TempData["name"];
return View();
}
Hope that works.
This is the classic Post-Redirect-Get pattern (PRG) and it looks fine but I would add one bit of code. In the DisplayStudent method check if your TempData variables are not null otherwise do a redirect to some default Index action. This is in case a user presses F5 to refresh the page.
public ActionResult DisplayStudent()
{
if(TempData["model"] == null)
{
return RedirectToAction("Index");
}
var model = (StudentResponseViewModel)TempData["model"];
return View(model);
}
public ViewResult Index()
{
IEnumerable<StudentResponseViewModel> students = GetAllStudents();
return View(students);
}
i have 2 actions
public ActionResult FilesAdd(int id)
{
FillParentMenuDDL(id);
return View();
}
[HttpPost]
public ActionResult FilesAdd(int id)
{
//some logic...
FillParentMenuDDL(id);
return View();
}
but it is error because of same parameters, but i need only one parameter. first i call page /action/id and then i submit it for example with id and uploaded file, but i access to file using request.files[0]. so what the solution with controllers and same parameters? i see only declare FilesAdd(int? id) in one controller
.Net MVC has an ActionNameAttribute for this purpose. Rename your second action to something like FilesAddPost and then use ActionNameAttribute("FilesAdd")
public ActionResult FilesAdd(int id)
{
FillParentMenuDDL(id);
return View();
}
[HttpPost]
[ActionName("FilesAdd")]
public ActionResult FilesAddPost(int id)
{
//some logic...
FillParentMenuDDL(id);
return View();
}
Add an (unused) form parameter to the POST action. That will make the method signatures different.
[HttpPost]
public ActionResult FilesAdd(int id, FormCollection form)
{
//some logic...
FillParentMenuDDL(id);
return View();
}
You can control the action of the submitted form, it doesn't have to go to the same action.
// Works under MVC 2.0
<% using (Html.BeginForm("action", "controller", FormMethod.Post)) { %>
// code
<% } %>