How to call an ActionResult from another ActionResult in asp.net - asp.net-mvc

I am creating a website for User registeration,display,login etc. I am currently trying to display the details of the user who have signed in. But within the actionResult of login I don't know how will i call the actionResult of display? I am new to asp.net. I need suggestions
public ActionResult login()
{
try
{
return View();
}
catch (Exception ex)
{
throw ex;
}
}
[HttpPost]
public ActionResult login(DEntities.Users user)
{
try
{
services.CheckUser(user);
controlsuccess = services.servicesuccess;
if (controlsuccess == true)
{
return RedirectToAction("display");
//return View("display");
}
else
{
return HttpNotFound();
}
}
catch (Exception ex)
{
throw ex;
}
}
public ActionResult display()
{
return View();
}
[HttpPost]
public ActionResult display(int id = 0)
{
try
{
DEntities.Users user = services.GetUserbyId(id);
return View(user);
}
catch (Exception ex)
{
throw ex;
}
}

Remove the [HttpPost] attribute from the display action.
If both actions are in the same controller, then just pass the action name:
return RedirectToAction("display", new { id = 1 });
Or if the actions are in different controllers, pass the action and controller names:
return RedirectToAction("display", "controllername", new { id = 1 });
Or if it is necessary to use [HttpPost], you can learn how to
RedirectToAction to a POST Action.

Related

If no data in database then how to show "nodata" message in asp.net core

How to show No have any Data message in List view page if no have any record in the database.
This is my ICouponRepository interface to inject in this controller.
IEnumerable<ManuItem> GetAllManuItem();
This is my SQLManuItemRepository to inherit ICouponRepository
public IEnumerable<ManuItem> GetAllManuItem()
{
try
{
return context.ManuItems.Include(mi => mi.Category)
.Include(mi => mi.SubCategory)
.Where(mi => mi.IsDelete == false);
}
catch (Exception ex)
{
var exception = ex.Message;
return null;
}
}
This is my Controller method
public IActionResult Index()
{
try
{
var model = manuItemRepository.GetAllManuItem();
if (model == null)
{
ViewBag.NoData = "No have any Data";
return View();
}
return View(model);
}
catch (Exception ex)
{
ViewBag.Error = "Somthing was Worng" + ex.Message;
}
return View();
}
Any change in this method or change in View?
You can directly judge whether the returned model has data on the view. You can refer to the modified code below.
Controller
public IActionResult Index()
{
var model = new List<ManuItem>();
try
{
model = manuItemRepository.GetAllManuItem();
}
catch (Exception ex)
{
ViewBag.Error = "Somthing was Worng" + ex.Message;
}
return View(model);
}
View
#if (Model.Count() == 0)
{
<p>No have any Data</p>
}
else
{
<p>Have Data</p>
}

Return Action as result of another action?

public ActionResult Index(int requestid)
{
return View(db.RequestListDetails.Where(c=>c.RequestID == requestid).ToList());
}
How can I back to View(db.RequestListDetails.Where(c=>c.RequestID == requestid).ToList()); from Create action.
My Create action code like this
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "....")] RequestListDetail requestListDetail)
{
if (ModelState.IsValid)
{
db.RequestListDetails.Add(requestListDetail);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(requestListDetail);
}
After db.SaveChanges() you could use this:
return RedirectToAction("Index", new { requestid = requestListDetail.RequestId });

put operations CRUD on a single file in asp.net

**Hello
I am developing an application with ASP.NET API, i created the controller entity and the MVC controllers that my generated CRUD operations in the view as follows (see picture), each operation in a file how to put it all in a single file.
**
enter image description here
controller code:
// GET: /Clients/
public ActionResult Index()
{
return View(db.CLIENT.ToList());
}
// GET: /Clients/Details/5
public ActionResult Details(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Client_H Client_H = db.Client_H.Find(id);
if (Client_H == null)
{
return HttpNotFound();
}
return View(Client_H);
}
// GET: /Clients/Create
public ActionResult Create()
{
return View();
}
// POST: /Clients/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="ID,nom,prenom,CODE,ADRESSE,BQE_VILLE,ADRESSE,TEL")] Client_H Client_H)
{
if (ModelState.IsValid)
{
db.Client_H.Add(Client_H);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(Client_H);
}
// GET: /Clients/Edit/5
public ActionResult Edit(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Client_H Client_H = db.Client_H.Find(id);
if (Client_H == null)
{
return HttpNotFound();
}
return View(Client_H);
}
// POST: /Clients/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="ID,nom,prenom,CODE,ADRESSE,BQE_VILLE,ADRESSE,TEL")] Client_H Client_H)
{
if (ModelState.IsValid)
{
db.Entry(Client_H).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(Client_H);
}
// GET: /Clients/Delete/5
public ActionResult Delete(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Client_H Client_H = db.Client_H.Find(id);
if (Client_H == null)
{
return HttpNotFound();
}
return View(Client_H);
}
// POST: /Clients/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(long id)
{
Client_H Client_H = db.Client_H.Find(id);
db.Client_H.Remove(Client_H);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
Thank you in advance
I do not understand well what do you mean by "how to put it all in a single file"?
If you want to have all CRUD function in on 1 view, you can use jquery POST & GET.
https://api.jquery.com/jquery.post/
https://api.jquery.com/jquery.get/

Print Function using Rotativa

i used Rotativa to print one of my page from html to pdf and when i hit the button to print,it will open print preview,but the preview does not contains that page i just print , it contains and show me log in page in preview instead as you can see,so idont know exactly whats happend.Can someone please point me in the right direction?
The page i tried to print look like this:
But when i hit the button to print,in preview give me log in page:
AccountController:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (Session["CustomerID"] == null &! Request.RawUrl.ToLower().Contains("login"))
{
Response.Redirect("/Account/Login");
}
base.OnActionExecuting(filterContext);
}
<br>
//Login
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(Customers cust)
{
using (DataContext db = new DataContext())
{
var user = db.Customers.Where(u => u.CustomerID == cust.CustomerID).FirstOrDefault();
if (user != null)
{
Session["CustomerID"] = user.CustomerID.ToString();
FormsAuthentication.SetAuthCookie(cust.CustomerID, false);
Session.Timeout = 30;
return RedirectToAction("LoggedIn");
}
else
{
ModelState.AddModelError("", "CustomerID is not Valid");
}
}
return View();
}
public ActionResult LoggedIn()
{
if (Session["CustomerID"] != null)
{
string customerId = Session["CustomerID"].ToString();
List<Orders> customerOrders;
using (DataContext data = new DataContext())
{
customerOrders = data.Orders.Where(x => x.CustomerID == customerId).ToList();
}
return View(customerOrders);
}
else
{
return RedirectToAction("Login");
}
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
Session.Remove("Login");
return RedirectToAction("Login", "Account");
}
public ActionResult PrintOrders(int id)
{
var report = new ActionAsPdf("ShowOrdersDetails", new { id = id });
return report;
}

How to serve the View/Action with parameters from different controller without redirecting?

How to do it? Currently my code is like this:
public ActionResult Index()
{
try
{
return RedirectToAction("Page", "Content", new { url = "/Home.html" });
}
catch(Exception ex)
{
return View();
}
}

Resources