简体   繁体   中英

fetch data from the given id in the controller asp.net mvc

I need to fetch a string from the database which is saved to it when adding a business in my case. I was able to save it to the db via below code in business controller

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,Title,Address,Category,Description,Latitude,Longitute,Owner")] Business business)
    {
        business.Owner = System.Web.HttpContext.Current.User.Identity.Name;//I'm saving the current user as the owner
        if (ModelState.IsValid)
        {
            db.Businesses.Add(business);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(business);
    }

Now all I need is to check whether the current user user is the owner of the business which is saved in the model when adding a business as seen in the above code. the model class is below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebPortal.Models
{
    public class Business
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Address { get; set; }
        public string Category { get; set; }
        public string Description { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }    
        public string Owner { get; set; }//in order to detect the original creator of the businness
    }
}

The saving part works fine and here's is the code which i tried to fetch it in the business controller

// GET: Businesses/Edit/5
    [Authorize]
    public ActionResult Edit([Bind(Include = "ID,Title,Address,Category,Description,Latitude,Longitute,Owner")] int? id, string ownerr, Business business)
    {
        Business bs = new Business();
        //Authorizing Edit permission only for the owner of the business and the admin
        if (!((System.Web.HttpContext.Current.User.Identity.Name == bs.Owner
                || User.Identity.Name == "admin@gmail.com" )))
        {
            return View(db.Businesses.ToList());
        }

It's kinda wronge. i just need to know how the fetch the relavent owner of the business by passing the ID maybe...

Edited

Id could be get via below html and i was trying to pass the owner as well but it returns a null in the controller

{
            @Html.ActionLink("| Edit | ", "Edit", new { id = item.ID, owner = item.Owner }) 
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        }

I usually make Identity Id the Id of the models and I use the ApplicationUserManager to query the database for the currently Signed In user

 private ApplicationUserManager _userManager;
          public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }


var user =  UserManager.FindById(User.Identity.GetUserId());
var userId = Guid.Parse(user.Id);

var _context = new MessageContext();               
var myContacts = _context.Contacts.Where(c => c.CustomerId == userId).ToList();
                 ViewBag.Contacts = myContacts;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM