创建/分配给模型的新视图需要 mvc 建议吗?

发布于 2024-12-07 01:49:31 字数 1096 浏览 4 评论 0原文

我对设置 mvc 还很陌生,请耐心等待.. 所以我有这两个模型

public class Promoter
 {
    public int Id {get; set;}
    public string Name {get;set;}

    public ICollection<Event> AllEvents {get;set;}
 }

public class Event
 {
    public int Id {get;set;}
    public string Name {get;set;}
    public date Date {get;set;}
    public int PromoterId {get; set;}

    public virtual Promoter Promoter {get;set;}
 }

并且我有一个控制器事件

public class EventsController : Controller
{
   public ActionResult New()
   {
        return View()
   }
   [HttpPost]
   public ActionResult New(Model model)
   {
       // do stuff to save the new Event related to Promotion
   }
}

所以我的问题是,在新操作上,我如何确保新视图能够有与哪个促销员相关的标识符吗?我认为隐藏的有 PromotionId,这样当帖子被调用时,Id 就会在那里,但是当我调用 New 时,我必须传递促销员 Id行动? 或者您认为我应该在访问该页面之前将促销员详细信息存储在 cookie 中? 另一件需要注意的事情是,用户将经过身份验证,并且需要登录才能访问促销控制器(此线程中未显示),然后在该控制器上用户可以转到说事件控制器将有新的行动......

我希望这是有道理的。 非常感谢需要一些建议、想法/想法。

谢谢,

G

I'm pretty new in setting up mvc, please be patient..
So I have these two models

public class Promoter
 {
    public int Id {get; set;}
    public string Name {get;set;}

    public ICollection<Event> AllEvents {get;set;}
 }

public class Event
 {
    public int Id {get;set;}
    public string Name {get;set;}
    public date Date {get;set;}
    public int PromoterId {get; set;}

    public virtual Promoter Promoter {get;set;}
 }

And I have a controller Events

public class EventsController : Controller
{
   public ActionResult New()
   {
        return View()
   }
   [HttpPost]
   public ActionResult New(Model model)
   {
       // do stuff to save the new Event related to Promotion
   }
}

so my problem is that, on the New Action, how do I make sure that the New View will be able to have an identifier on which Promoter it will be related to? I'm thinking hiddenfor that has the PromotionId so that when the Post gets called the Id will be there, but I will have to pass the Promoter Id when I call the New action?
Or do you think I should store the Promoter details in a cookie before going to that page?
Another thing to take note, the user will be authenticated and will need to be logged in to be able to access the Promotion controller (not shown in this thread) and then on that controller user can go to the said Event Controller which will have the New Action...

I hope that makes sense.
Need some advice, thoughts / ideas is very much appreciated.

THanks,

G

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念绕指尖 2024-12-14 01:49:31

您的想法是对的,您需要将 PromotionId 传递给新操作,而操作需要将其传递给视图。也可以通过cookie,但我认为action参数更方便 - 因为action参数将在url中,所以用户甚至可以将其添加为书签,发送给另一个用户等。

You're thinking right, you need to pass promotionId to the new action and action needs to pass it to the view. It is also possible through cookie, but I think action parameter is more convenient - because action parameter will be in url, so user can even bookmark it, send to another user, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文