从控制器asp.net中的给定ID获取数据mvc

我需要从数据库中获取一个字符串,在我的案例中添加业务时将其保存到该数据库中。我可以通过下面的代码,将其保存到数据库中的业务控制器从控制器asp.net中的给定ID获取数据mvc

[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);

}

现在,所有我需要的是检查当前用户的用户是否是被添加业务时,保存在模型中的业务所有者如上面的代码所示。模型类低于

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

}

}

的节省部分工作正常,这里的是我试图把它的业务控制器

// 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());

}

它还挺wronge获取代码。我只需要知道通过传递也许是ID如何获取企业的relavent所有者...

编辑

ID可以通过以下HTML获取和我正在试图通过owner以及但是它在控制器返回一个空

{ 

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

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

}

回答:

我一般都身份标识模型的Id和我使用ApplicationUserManager查询数据库为当前登录用户

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;

以上是 从控制器asp.net中的给定ID获取数据mvc 的全部内容, 来源链接: utcz.com/qa/257938.html

回到顶部