I need two differend views for Kaminy/KaminniTopky & Kaminy/KaminniTopky/5.
How can I do it in that way?
public class KaminyController : Controller
{
//
// GET: /Kaminy/
public ActionResult Index()
{
return View();
}
// GET: /Kaminy/KaminniTopky
public ActionResult KaminniTopky()
{
return View();
}
// GET: /Kaminy/KaminniTopky/5
public ActionResult KaminniTopky(int id)
{
return View();
}
}
#Stephen Muecke's idea has merit, you can combine as:
public ActionResult KaminniTopky(int? id)
{
if (!id.HasValue)
{
return View();
}
else
{
return View("OtherViewName");
}
}
There are overloads that take the model too - return View(someModel); and return View("OtherViewName", someModel); respectively.
In MVC 5 you could:
[Route("Kaminy/KaminniTopky")]
public ActionResult KaminniTopky()
{
return View();
}
[Route("Kaminy/KaminniTopky/{id}")]
public ActionResult KaminniTopky(int id)
{
return View("OtherViewName");
}
For something like /Kaminy/KaminniTopky/KaminniTopkyWithOther/5 you would use routing rather than a weird controller structure to support other style URLs.
E.g. In RouteConfig:
routes.MapRoute(
"SomeRoute",
"/Kaminy/KaminniTopky/KaminniTopkyWithOther/{id}",
new { controller = "Kaminy", action = "KaminniTopkyWithOther", id = "" }
);
Make sure this comes before the default route.
Then it doesn't matter what your action is called as long as its the same as defined in the route.
public ActionResult KaminniTopkyWithOther(int id)
{
return View("OtherViewName");
}
Related
I am working a project and I had some trouble. I want to send id from html beginform but I couldn't do it.
I want to send /BuyTourTicket/tourid
This is my code:
public ActionResult TourTicket(int id)
{
var tour = db.TBLTUR.Find(id);
ViewBag.tourid = tour.id;
ViewBag.tourname = tour.tur_basligi.ToString();
ViewBag.kalkisYeri = tour.kalkis_yeri.ToString();
ViewBag.tarih = tour.tarih.ToString();
ViewBag.detaylar = tour.detay.ToString();
ViewBag.turYetkilisi = db.TBLTURYETKILISI.Find(id).ad + " " + db.TBLTURYETKILISI.Find(id).soyad;
return View("TourTicket",tour);
}
public ActionResult BuyTourTicket()
{
return View();
}
[HttpPost]
public ActionResult BuyTourTicket(int id)
{
TBLTURREZERVASYON reservation = new TBLTURREZERVASYON();
reservation.tur = id;
db.TBLTURREZERVASYON.Add(reservation);
db.SaveChanges();
return View();
}
This is the error I get:
The first, the default route usually describes the id parameter as optional. Therefore change the action method declaration to public ActionResult BuyTourTicket(int? id):
[HttpPost]
public ActionResult BuyTourTicket(int? id)
{
TBLTURREZERVASYON reservation = new TBLTURREZERVASYON();
reservation.tur = id;
db.TBLTURREZERVASYON.Add(reservation);
db.SaveChanges();
return View();
}
The second, change the parameters order in the Html.BeginForm() of the TourTicket.cshtml to:
#using (Html.BeginForm("BuyTourTicket", "Tur", new { id = ViewBag.tourid }, FormMethod.Post))
The route values are third parameter and the HTTP method is the fourth parameter.
I would fix an action route
[HttpPost("{id"})]
public ActionResult BuyTourTicket(int id)
and add Get to another action
[HttGet]
public ActionResult BuyTourTicket()
{
return View();
}
I have a simply controller to CRUD operations. All actions works expect the last action named Delete which is HTTP DELETE action. When I try call delete action with example url:
http://localhost/api/groups/1/attendances/10
then application returns 404 Not Found and action is not firing.
In my other controllers delete action works correctly. One difference is that in others controllers I have one route attribute on controller instead of on each action. This is a problem?
public class AttendancesController : Controller
{
public AttendancesController(IGroupService groupService, IAttendanceService attendanceService, IPersonService personService, IPersonAttendanceService personAttendanceService)
{
//
}
[Route("api/groups/{groupId}/[controller]")]
[HttpGet]
public IActionResult GetAttendancesForGroup(int groupId)
{
//
}
[Route("api/groups/{groupId}/[controller]/{date}")]
[HttpGet]
public IActionResult GetAttendanceForGroup(int groupId, DateTime date)
{
//
}
[Route("api/groups/{groupId}/[controller]")]
[HttpPost]
public IActionResult CreateAttendanceForGroup(int groupId, [FromBody] AttendanceCreateDto dto)
{
//
}
[Route("api/people/{personId}/[controller]")]
[HttpGet]
public IActionResult GetAttendancesForPerson(int personId)
{
//
}
[Route("api/groups/{groupId}/[controller]")]
[HttpDelete("{id}")]
public IActionResult Delete(int groupId, int id)
{
var group = _groupService.FindById(groupId);
if (group == null)
return NotFound();
var attendance = _attendanceService.GetAttendanceByIdAndGroupId(id,groupId);
if (attendance == null)
return NotFound();
_attendanceService.Delete(attendance);
return NoContent();
}
}
I dont understand why in this case [HttpDelete("{id}")] is ignored.
You are mixing routes.
Refactor the class as follows.
Add the common route to the controller as a route prefix and also take advantage to route constraints
[Route("api/groups/{groupId}/[controller]")]
public class AttendancesController : Controller {
public AttendancesController(IGroupService groupService, IAttendanceService attendanceService, IPersonService personService, IPersonAttendanceService personAttendanceService) {
//
}
[HttpGet] // Matches GET api/groups/1/attendances
public IActionResult GetAttendancesForGroup(int groupId) {
//
}
[HttpGet("{date:datetime}")] //Matches GET api/groups/1/attendances/2017-05-27
public IActionResult GetAttendanceForGroup(int groupId, DateTime date) {
//
}
[HttpPost] // Matches POST api/groups/1/attendances
public IActionResult CreateAttendanceForGroup(int groupId, [FromBody] AttendanceCreateDto dto) {
//
}
[HttpGet("~/api/people/{personId}/[controller]")] // Matches GET api/people/1/attendances
public IActionResult GetAttendancesForPerson(int personId) {
//
}
[HttpDelete("{id:int}")] // Matches DELETE api/groups/1/attendances/10
public IActionResult Delete(int groupId, int id) {
var group = _groupService.FindById(groupId);
if (group == null)
return NotFound();
var attendance = _attendanceService.GetAttendanceByIdAndGroupId(id,groupId);
if (attendance == null)
return NotFound();
_attendanceService.Delete(attendance);
return NoContent();
}
}
Now I have an action A and an action B,I want to jump to action B in action A.But action B has
a parameter "MyModel".If I write like this:
public ActionResult A(MyModel model)
{
...
return B(model);
}
public ActionResult B(MyModel model)
{
...
return View();
}
It will cause an error.
What should I do to transfer the parameter "MyModel" to action B in action A?
Do this:
public ActionResult A(MyModel model)
{
return RedirectToAction("B", model);
}
Try this
public ActionResult A(MyModel model)
{
...
TempData["object"] = new { Model= model };
return RedirectToAction("B");
}
Then in your B action
public ActionResult B(MyModel model)
{
...
var yourObj = TempData["object"];
model=yourObj .Model;
return View();
}
Why do i get this error
The current request for action 'Index' on controller type 'MyController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index() on type MyProj.Controllers.MyController
System.Web.Mvc.ActionResult Index(MyProj.Models.MyModel) on type MyProj.Controllers.MyController
COntroller Class :
public class MyController : Controller
{
//
// GET: //
public ActionResult Index()
{
return View();
}
public ActionResult Index(MyModel model)
{
string x = "Hello "+ model.name;
return View();
}
}
}
They are both GET actions.
Add this before your second method:
[HttpPost]
Like so:
public class MyController : Controller
{
//
// GET: //
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MyModel model)
{
string x = "Hello "+ model.name;
return View();
}
}
If you want to overload you need to add attribute to change your method name:
[ActionName("OverloadedName")]
Why isn't this working. It keeps telling me edit needs a EditStudyModel when I redirect in my Create method. A Redirect should do a get, right?
public ViewResult Create()
{
var createStudyModel = new CreateStudyModel();
return View(createStudyModel);
}
[HttpPost]
public ActionResult Create(CreateStudyModel createStudyModel)
{
try
{
//TODO: Send CreateStudyCommand
return RedirectToAction("Edit", new { scientificStudyId = new Guid("{1C965285-788A-4B67-9894-3D0D46949F11}") });
}
catch
{
return View(createStudyModel);
}
}
[GET("ScientificStudy/Create/{scientificStudyId}")]
public ActionResult Edit(Guid scientificStudyId)
{
//TODO: Query real model
var model = new EditStudyModel() {StudyNr = "T01", StudyName = "Test"};
return View(model);
}
[HttpPost]
public ActionResult Edit(EditStudyModel editStudyModel)
{
try
{
//TODO: Send UpdateStudyCommand
return RedirectToAction(""); //TODO: Terug naar Studie lijst
}
catch
{
return View(editStudyModel);
}
}
You're returning a redirect with a URL as a string, the controller isn't able to parse the GUID and convert it back to a guid object, so it's not able to resolve the correct method signature to use. Change it to:
return RedirectToAction("Edit", new { scientificStudyId = "{1C965285-788A-4B67-9894-3D0D46949F11}" });
and
public ActionResult Edit(string scientificStudyId)
I found the issue. I copy paste my create.cshtml to edit.cshtml Forgot to change the first line:
#model Website.Models.CreateStudyModel --> to --> #model Website.Models.EditStudyModel