1

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 == "[email protected]" )))
        {
            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 })
        }

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

4 Comments

hi @frank there's no problem getting the id it's simply achieved via the edited code at the bottom in the question. all I need is to get the relavent owner for each id...
Do you have an Owner model?
So what value are you currently storing as owner, a Name of a person?
am saving the person who post it to the website as the owner so that only he could grant permission to edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.