How can send dropdownlist value from Controller to another controller - c#

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;
}

Related

Multiple GET() methods with a single parameter result in AmbiguousMatchException: The request matched multiple endpoints

[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
}
[HttpGet("{id}")]
public ActionResult<string> Get([FromRoute]int id)
{
}
[HttpGet]
public ActionResult<IEnumerable<string>> Get()([FromQuery]DateTime dateTime)
{
}
I can reach the second with:
https://localhost:44341/api/Orders/3
But for the first and third:
https://localhost:44341/api/Orders
https://localhost:44341/api/Orders?dateTime=2019-11-01T00:00:00
Both of these return the error:
AmbiguousMatchException
Core 2.2, if it matters.
I ended up just creating a different endpoint for the GetByDate method.
[HttpGet]
public ActionResult<string> Get()
{
//
}
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
//
}
[HttpGet("ByDate/{date}")]
public ActionResult<string> ByDate(DateTime date)
{
//
}
They can be called as follows:
https://localhost:44341/api/controller
https://localhost:44341/api/controller/1
https://localhost:44341/api/controller/getbydate/2019-11-01
We can have a route at the controller level and handle the string input to parse to int or date as below
[Route("api/[controller]/{id}")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values/5
[HttpGet()]
public ActionResult<string> Get(string id)
{
if (int.TryParse(id, out int result))
{
return Ok(id);
}
else if (DateTime.TryParse(id, out DateTime result1))
{
return Ok(id);
}
else
return Ok("Failed");
}
}
How about having Id and date as optional parameters? You can still handle the actual search in a seprate methods if you want, but you have one GET method.
[HttpGet("{id}")]
public IActionResult Get([FromRoute]int? id [FromQuery]DateTime? dateTime)
{
if(id.HasValue)
{
//Do Something with the id
}
else if (dateTime.HasValue)
{
//Do Something with the date
}
else
{
//return all
}
}

insert data into dictionary [C#]

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

How do i enable cache in my RestAPI in c#?

This is my EmployeeDetailsController.cs
namespace EmpApi.Controllers
{
[RoutePrefix("")]
public class EmployeeDetailsController : ApiController
{
[HttpGet]
[Route("Employees")]
public IEnumerable<Employee> Employees()
{
}
[HttpGet]
[Route("Details/{id}")]
public IEnumerable<Details> Details(int id)
{
}
[HttpGet]
[Route("TeamInfo/{id}")]
public IEnumerable<Team> TeamInfo(int id)
{
}
[HttpGet]
[Route("DetailsForTeam/{id}")]
public IEnumerable<Details> DetailsForTeam(int id)
{
;
}
[HttpPost]
[Route("PostEmp")]
public void PostEmp([FromBody] Employee cs)
{
}
[HttpPut]
[Route("PutEmp/{id}")]
public void PutEmp(int id, [FromBody]Employee cs)
{
}
[HttpDelete]
[Route("DeleteEmp/{id}")]
public void DeleteEmp(int id)
{
}
}
}
I made an API which has various services.
Suppose i call api/Employees,after i call api/Details/12 and then when i click GoBack button in browser, api/Employees should not be triggered.
How do i enable cache for my API.Please tell me the steps as I am new in WebApI.
Thanks in advance..
Add this code before your controller declaration as follows:
[OutputCache(VaryByParam = "*", Duration = 0, NoStore = true)]
public class EmployeeDetailsController : ApiController
{
...
}

How to use dependency to mock data. MVC5

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.

cart checkout price add to payfast

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

Categories