public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
//var clinicbusiness = new ClinicBusiness ();
return View();
}
[HttpPost]
public ActionResult ValidateCommand()
{
var pa = new PaymentView();
if (ModelState.IsValid)
return View("ValidateCommand");
else
return View();
}
public ActionResult RedirectFromPaypal()
{
return View("RedirectFromPaypal");
}
private ActionResult View(Func<ActionResult> RedirectFromPaypal)
{
throw new NotImplementedException();
}
this is my controller and i have a problem taking items from A CART
how to add cart items to a controller n accumulate price
using payfast checkout button
Related
I have a problem: how can I solve the problem that I can not insert entries into the dictionary (this is the C # programming language).
What to do about it? And how can I solve the problem?
Code fragment:
public class CustomerController : Controller
{
public static int CTR = 1;
public Customer C { get; set; }
public Dictionary<int,Customer> myd = new Dictionary<int, Customer>();
// GET: Customer
public ActionResult Index()
{
if (CTR > 1)
{
C = myd.Last().Value;
return View(C);
}
else
{
return View();
}
}
public ActionResult Add()
{
return View();
}
public ActionResult Submit()
{
Customer c = this.C;
CTR++;
myd.Add(CTR,c);
return View("Index");
}
public ActionResult ShowAll()
{
return View(myd.Last().Value);
}
}
MVC controller is created each time a new Web request hits it, so your fields will always be re-created. Consider using static fields
I am new to MVC5 and i am trying to work on 'search' functionality. my aim is get data from a dataservice.(that is, i enter data into a form and hit the search button, if theres a record it displays data).
I have created a dependency to mock the data(dummy data). How do i wire up my code to the contoller to achieve my purpose?. Please advice Thank you.
Heres my controller and my mock:
public class SearchController : Controller
{
private readonly ISearchResultsService _resultsService;
public SearchController() : this(DependencyFactory.NewResultsService())
{
}
public SearchController(ISearchResultsService resultsService)
{
_resultsService = resultsService;
}
// GET:Search
public ActionResult Index()
{
return View();
}
// GET: Search/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Search/Create
public ActionResult Create()
{
return View();
}
// POST: Search/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return();
}
catch
{
return View();
}
}
//[HttpPost]
//public ActionResult Index(SearchCriteria data)
//{
// var data = this._resultsService.FindClaims(data);
// return View(data);
//}
[HttpPost]
public ActionResult Index()
{
return View();
}
// GET: Search/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: Search/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Search/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Search/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
public static class DependencyFactory
{
public static ISearchResultsService NewResultsService()
{
return new MockSearchResultsService();
}
}
public interface ISearchResultsService
{
List<Person> FindClaims(string firstName, string lastName);
}
public class MockSearchResultsService : ISearchResultsService
{
public List<Person> FindClaims(string firstName, string lastName)
{
return new List<Person>(new []{new Person{FirstName = "John", LastName = "Doe"}});//throw new NotImplementedException();
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
I recommend you to use some IoC container like http://autofac.org. You can configure your container and register your service like this way:
builder.RegisterType<MockSearchResultsService>().As<ISearchResultsService>()
All your dependencies will be automatically resolved:
public class SearchController : Controller
{
private readonly ISearchResultsService _resultsService;
public SearchController(ISearchResultsService resultsService)
{
_resultsService = resultsService;
}
}
Note that there is many IoC containers you can use. Try to search more about AutoFac, Ninject or Castle Windsor.
I want to send dropdownlist selected value from Controller to another controller
and recuperate that value (i know how to send it ) but i dont know how to recuperat it
controller 1:
public class PosteController : Controller
{
[HttpPost]
public ActionResult Index(CandidateModel Id)
{
return RedirectToAction ("Inscription","Candidate",Id);
}
public class CandidateController : Controller
{
[HttpPost]
public ActionResult Inscription()
{
...........
}
Method 1:
Using Object Route :
public class PosteController : Controller
{
[HttpPost]
public ActionResult Index(CandidateModel Id)
{
return RedirectToAction ("Inscription","Candidate",new{ dropdownval=Id.val,Id });
}
public class CandidateController : Controller
{
public ActionResult Inscription(int? dropdownval)
{
...........
}
Method 2:
Using TempData:
public class PosteController : Controller
{
[HttpPost]
public ActionResult Index(CandidateModel Id)
{
TempData["Id"]=Id.val;
return RedirectToAction ("Inscription","Candidate");
}
public class CandidateController : Controller
{
public ActionResult Inscription()
{
var id=TempData["Id"];
...........
}
In above examples Id.val is the selected dropdown value.
You can use TempData for this.
[HttpPost]
public ActionResult Index(CandidateModel Id)
{
TempData["var"] = id;
return RedirectToAction ("Inscription","Candidate",Id);
}
public ActionResult Inscription()
{
var id = TempData["id"] as CandidateModel;
}
i have used custom Membershipprovider and roleprovider class in my asp.net mvc4 application.
this code works fine :
[OutputCache(Duration =0, NoStore= true)]
public class HomeController : Controller
{
public ActionResult Login(string ReturnUrl)
{
return View(new User());
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(User u, string ReturnUrl) {
if (Membership.ValidateUser(u.login, u.password))
{
FormsAuthentication.SetAuthCookie(u.login, false);
if (Roles.GetRolesForUser(u.login).Contains("user")) return RedirectToAction("Index");
else return RedirectToAction("Common");
}
else {
return View(u);
}
}
[Authorize(Roles = "user")]
public ActionResult Index()
{
return View();
}
[Authorize(Roles="admin")]
public ActionResult Common()
{
return View();
}
public ActionResult SignOut()
{
FormsAuthentication.SignOut();
return RedirectToAction("Login");
}
}
But i need to verify if the existence of a connected user in the Login action
public ActionResult Login(string ReturnUrl)
{
//verification and redirection if connected
return View(new User());
}
I need to know:
What are the different manners to do that?
What is the best one?
you can check this way:
if (User.Identity.IsAuthenticated)
{
// user logged in already
}
This is homework, an ASP.NET MVC app in Visual Studio using C#. When I run it, the error says, "Cannot implicitly convert type 'string' to 'int'," referring to the line: Manufacturer = collection["Manufacturer"], Gears = collection["Gears"], Frame = collection["Frame"] and there's a squiggly line under Gears = collection["Gears"].
using MvcApplication3.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication3.Controllers
{
public class BikeController : Controller
{
//
// GET: /Bike/
List<Bike> bikes;
public BikeController()
{
if (bikes == null)
{
bikes = new List<Bike> {
new Bike(),
new Bike { Manufacturer = "Nishiki", Gears = 5, Frame = "Road" }
};
}
}
public ActionResult Index()
{
return View(this.bikes);
}
private ActionResult View(Func<object> func)
{
throw new NotImplementedException();
}
//
// GET: /Bike/Details/5
public ActionResult Details(int id)
{
var currentBikes = bikes[id];
return View(currentBikes);
}
//
// GET: /Bike/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Bike/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
Bike b = new Bike
{
Manufacturer = collection["Manufacturer"], Gears = collection["Gears"], Frame = collection["Frame"]
};
bikes.Add(b);
try
{
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Bike/Edit/5
public ActionResult Edit(int id)
{
return View(bikes.Where(b => b.BikeID == id).First());
}
//
// POST: /Bike/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Bike/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /Bike/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public int bike { get; set; }
}
}
You need to explicitly convert string to int.
Try following line
Manufacturer = collection["Manufacturer"], Gears = int.Parse(collection["Gears"]), Frame = collection["Frame"]