Javascript 在视图MVC5之间传递值

Javascript 在视图MVC5之间传递值,javascript,c#,asp.net,asp.net-mvc,razor,Javascript,C#,Asp.net,Asp.net Mvc,Razor,我正在创建一个事件管理系统,我想创建一个事件,然后为此事件创建多个票证。我正在使用c#和ASP.NET MVC。我创建了这些模型类 public class Event { public int EventID { get; set; } [Required] public String Name { get; set; } [Required] public String Location { get; set; } [Required]

我正在创建一个事件管理系统,我想创建一个事件,然后为此事件创建多个票证。我正在使用c#和ASP.NET MVC。我创建了这些模型类

public class Event
{
    public int EventID { get; set; }
    [Required]
    public String Name { get; set; }
    [Required]
    public String Location { get; set; }
    [Required]
    public DateTime Date { get; set; }
    [Required]
    [DataType(DataType.MultilineText)]
    public String Description { get; set; }
    [Required]
    public int TicketsAvailable { get; set; }
    //navigation property
    public virtual ICollection<Order> Order { get; set; }
    //navigation property
    public virtual ICollection<Ticket> Ticket { get; set;}
 }

  public class Ticket
{
   public int TicketID { get; set; }
   [Required]
   [ForeignKey("Event")]
   //foreign key
    public int EventID { get; set; }
   [Required]
   public string Description { get; set; }
   [Required]
   public float Price { get; set; }       
    //navigation property
    public virtual Event Event { get; set; }
    //navigation property
    public ICollection<OrderDetails> OrderDetails { get; set; }
}
公共类事件
{
public int EventID{get;set;}
[必需]
公共字符串名称{get;set;}
[必需]
公共字符串位置{get;set;}
[必需]
公共日期时间日期{get;set;}
[必需]
[数据类型(DataType.multilitext)]
公共字符串说明{get;set;}
[必需]
公共整数票证可用{get;set;}
//导航属性
公共虚拟ICollection顺序{get;set;}
//导航属性
公共虚拟ICollection票证{get;set;}
}
公务舱票
{
public int TicketID{get;set;}
[必需]
[外键(“事件”)]
//外键
public int EventID{get;set;}
[必需]
公共字符串说明{get;set;}
[必需]
公开浮动价格{get;set;}
//导航属性
公共虚拟事件事件{get;set;}
//导航属性
公共ICollection OrderDetails{get;set;}
}
我已经为事件使用了搭建的CRUD视图,然后我想将我创建的事件的EventID传递到AddTicket视图,并创建特定于该事件的新票证。 这是我的控制器类

public class Events1Controller : Controller
{
    private IEventRepository _eventRepository;
    private ITicketRepository _ticketRepository;

    public Events1Controller()
    {
    this._eventRepository = new EventRepository(new ApplicationDbContext());
    this._ticketRepository = new TicketRepository(new ApplicationDbContext());
    }

    // GET: Events
    [AllowAnonymous]
    public ActionResult Index()
    {

        return View(_eventRepository.GetEvents());
    }
// GET: Events/Create
    [AllowAnonymous]
    public ActionResult Create()
    {
        return View();
    }

    // POST: Events/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]
    [AllowAnonymous]
    public ActionResult Create([Bind(Include = "EventID,Name,Location,Date,Description,TicketsAvailable")] Event @event)
    {
        if (ModelState.IsValid)
        {
            Session["Event1"] = @event;
            _eventRepository.InsertEvent(@event);

            return RedirectToAction("SaveTickets");
        }

        return View();
    }

    [AllowAnonymous]
    public ActionResult SaveTickets()
    {           
        Event @e1 = Session["Event1"] as Event;
        Ticket @ticket1 = new Ticket
        {
            EventID = @e1.EventID
        };

        return View(@ticket1);

    }



 // POST: Events/AddToTickets
    [HttpPost]
    [ValidateAntiForgeryToken]
    [AllowAnonymous]
    public ActionResult AddToTickets([Bind(Include = "TicketID, EventID,    Description, Price")] Ticket @ticket)
    {

        if (ModelState.IsValid)
        {
            _ticketRepository.InsertTicket(@ticket);
            return RedirectToAction("Index");
        }
        return View();
    }




<h2>Create</h2>

@using (Html.BeginForm())
{
 @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Event</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.TicketID)


    <div class="form-group">
        @Html.LabelFor(model => model.EventID, htmlAttributes: new { @class    = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DisplayFor(model => model.EventID, new { htmlAttributes =   new { @class = "form-control" } })

        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Price,"",  new { @class = "text-danger" })
        </div>
    </div>



    <div class="form-group">
        @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
        </div>
    </div>




    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="AddToTickets" class="btn btn-default" />

        </div>
    </div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")

</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
公共类事件1控制器:控制器
{
私人电子档案库;
私人ITicketRepository _ticketRepository;
公共事件1控制器()
{
此._eventRepository=neweventrepository(new ApplicationDbContext());
此._ticketposition=new ticketposition(new ApplicationDbContext());
}
//获取:事件
[异名]
公共行动结果索引()
{
返回视图(_eventRepository.GetEvents());
}
//获取:事件/创建
[异名]
公共操作结果创建()
{
返回视图();
}
//发布:事件/创建
//若要防止套印攻击,请启用要绑定到的特定属性,例如
//更多详细信息请参见http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[异名]
public ActionResult Create([Bind(Include=“EventID,Name,Location,Date,Description,TicketsAvailable”)]Event@Event)
{
if(ModelState.IsValid)
{
会话[“Event1”]=@event;
_eventRepository.InsertEvent(@event);
返回重定向操作(“保存票据”);
}
返回视图();
}
[异名]
公共行动结果保存票证()
{           
事件@e1=会话[“事件1”]作为事件;
票证@ticket1=新票证
{
EventID=@e1.EventID
};
返回视图(@ticket1);
}
//帖子:事件/添加主题
[HttpPost]
[ValidateAntiForgeryToken]
[异名]
public ActionResult AddToTickets([Bind(Include=“TicketID,EventID,Description,Price”)]Ticket@Ticket)
{
if(ModelState.IsValid)
{
_ticketposition.InsertTicket(@ticket);
返回操作(“索引”);
}
返回视图();
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
事件

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @Html.HiddenFor(model=>model.TicketID) @LabelFor(model=>model.EventID,htmlAttributes:new{@class=“controllabel col-md-2”}) @DisplayFor(model=>model.EventID,new{htmlAttributes=new{@class=“form control”}}) @LabelFor(model=>model.Price,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Price,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Price,“,new{@class=“text danger”}) @LabelFor(model=>model.Description,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Description,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Description,“,new{@class=“text danger”}) } @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }

我在razor视图中使用了表单,事件正确地保存到数据库中,并且EventID正在记录单中设置并传递到SaveTicket视图。但是,当我尝试保存记录单时,出现了问题,记录单没有保存,页面只会刷新。我尝试了许多教程,但都没有为我提供详细的说明解决方案还没有,我已经被困了一周的大部分时间。

你的问题可能是你没有告诉你的
在哪里发布数据

当您仅使用
@using(Html.beginnform())
时,它假定您希望将数据发回视图所来自的同一url,或地址栏中的内容

由于返回视图的操作是
SaveTickets
,因此数据将发布到
http://host/Events1/SaveTickets
但是您的
[HttpPost]
操作是
AddToTickets

您也可以将
[HttpPost]
操作重命名为
SaveTickets
,这将是最简单的,或者告诉表单您要发布到的url/操作是什么

@using (Html.BeginForm("AddToTickets","Events1"))

这可能会解决问题,但您希望采纳Stephen的建议,为票据创建一个单独的控制器。

您的问题可能是您没有告诉您的
将数据发布到哪里

当您仅使用
@using(Html.BeginForm())
时,它假定您希望将数据发布回