.NET MVC 3提交按钮做了不同的事情

时间:2012-03-31 02:12:37

标签: asp.net-mvc-3 input submit multiple-forms

有人可以向我解释为什么这不起作用?我在一个页面上阅读了很多关于多个提交按钮的帖子,但我不明白后台发生了什么。我试图按照我看到其他人在这些帖子中所做的事情,但似乎都没有。我只想了解这里到底发生了什么。我怀疑它与页面发布时确实发生的事情有关。

两个按钮完全不同。 第一个按钮发布表单。在表单的邮政编码中,然后将其重定向到车辆/索引第二个按钮不会发布表单,直接转到 / Vehicle / CreateNewVehiclePart

这就是我想要的。我希望两个按钮都能提交表单(因此我可以在继续之前执行验证并将更改保存到Vehicle)。保存车辆数据后,我想根据点击的按钮移动到 / Vehicle / AddPartsToVehicle / Vehicle / CreateNewVehiclePart

<table class="layouttable">
    <tr>
        <td>
            @using (Html.BeginForm("AddPartsToVehicle", "Vehicle", FormMethod.Post, null))
            {
                <input type="submit" value="Add Parts To Vehicle" />
            }
        </td>
        <td>
            @using (Html.BeginForm("CreateNewVehiclePart", "Vehicle", FormMethod.Post, null))
            {
                <input type="submit" value="Create New Vehicle Part" />
            }
        </td>
    </tr>
</table>

这是车型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Globalization;


namespace ShopLog.Models
{
    public class Vehicle
    {
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid VehicleID { get; set; }

    [Timestamp]
    public Byte[] Timestamp { get; set; }

    [Required]
    [Display(Name = "Vehicle Name")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string VehicleName { get; set; }

    [Display(Name = "Assigned To")]
    [MaxLength(30, ErrorMessage= "The {0} must be no more than {2} characters long.")]
    public string AssignedTo { get; set; }

    [Display(Name = "ID Number")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string IDNumber { get; set; }

    [Display(Name = "Active")]
    public bool Active { get; set; }

    [Display(Name = "Sign Out Vehicle")]
    public bool SignOutVehicle { get; set; }

    [Display(Name = "Have Title")]
    public bool HaveTitle { get; set; }

    [Display(Name = "Mileage or Hours")]
    public int? MileageOrHours { get; set; }

    [Display(Name = "Make")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string Make { get; set; }

    [Display(Name = "Model")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string Model { get; set; }

    [Display(Name = "Year")]
    public int? Year { get; set; }

    [Display(Name = "Engine")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string Engine { get; set; }

    [Display(Name = "Transmission Gears")]
    public int? TransmissionGears { get; set; }

    [Display(Name = "VIN")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string VIN { get; set; }

    [Display(Name = "Serial Number")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string SerialNumber { get; set; }

    [Display(Name = "Color")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string Color { get; set; }

    [Display(Name = "Air Filter")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string AirFilter { get; set; }

    [Display(Name = "Engine Oil Filter")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string EngineOilFilter { get; set; }

    [Display(Name = "Fuel Filter")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string FuelFilter { get; set; }

    [Display(Name = "Transmission Oil Filter")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string TransmissionOilFilter { get; set; }

    [Display(Name = "HydraulicOilFilter")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string HydraulicOilFilter { get; set; }

    [Display(Name = "Interior A/C Filter")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string InteriorACFilter { get; set; }

    [Display(Name = "Differential Oil")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string DifferentialOil { get; set; }

    [Display(Name = "Transmission Oil")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string TransmissionOil { get; set; }

    [Display(Name = "Engine Oil")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string EngineOil { get; set; }

    [Display(Name = "Tire Size")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string TireSize { get; set; }

    [Display(Name = "Tire Pressure")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string TirePressure { get; set; }

    [Display(Name = "Smog Due Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    public DateTime? SmogDue { get; set; }

    [Display(Name = "Registration Due Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    public DateTime? RegistrationDue { get; set; }

    [Display(Name = "Insurance Due Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    public DateTime? InsuranceDue { get; set; }

    [Display(Name = "License Plate")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string LicensePlate { get; set; }

    [Display(Name = "Owner")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string Owner { get; set; }

    [Display(Name = "Previous Owner")]
    [MaxLength(30, ErrorMessage = "The {0} must be no more than {2} characters long.")]
    public string PreviousOwner { get; set; }

    [Display(Name = "Date Acquired")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    public DateTime? DateAcquired { get; set; }

    [Display(Name = "Notes")]
    [DataType(DataType.MultilineText)]
    public string Notes { get; set; }

    [Required(ErrorMessage="Vehicle Type is required.")]
    public Guid VehicleTypeID { get; set; }
    public Guid? TransmissionTypeID { get; set; }

    //A vehicle can only have one VehicleType and one TransmissionType
    [Display(Name = "Vehicle Type")]
    public virtual VehicleType VehicleType { get; set; }

    [Display(Name = "Transmission Type")]
    public virtual TransmissionType TransmissionType { get; set; }


    //A vehicle can have many Parts
    [Display(Name = "Parts")]
    public virtual ICollection<Part> Parts { get; set; }

    //A vehicle can have many Warranties
    [Display(Name = "Warranties")]
    public virtual ICollection<Warranty> Warranties {get; set; }
}

}

以下是Part模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace ShopLog.Models
{
    public class Part
    {
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid PartID { get; set; }

    public string Name { get; set; }
    public decimal Cost { get; set; }

    [DataType(DataType.MultilineText)]
    public string Notes { get; set; }

    //A Part can belong to many Vehicles
    public virtual ICollection<Vehicle> Vehicles { get; set; }
    }
}

3 个答案:

答案 0 :(得分:1)

我在一个简单的项目中试过这个

@model MvcApplication2.Models.LogOnModel

@{
    ViewBag.Title = "Home Page";
}

@using (Html.BeginForm("About", "Home", FormMethod.Post, null))
{
    <input type="submit" name="btn2" value="About" />
    @Html.TextAreaFor(m => m.UserName)
}

@using (Html.BeginForm("Index", "Home", FormMethod.Post, null))
{        
    <input type="submit" name="btn1" value="Index" />
    @Html.TextAreaFor(m => m.UserName)
}

public class HomeController : Controller
{
    [HttpPost]
    public ActionResult Index(LogOnModel a)
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";


        return View();
    }

    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult About(LogOnModel a)
    {

        return View();
    }
}

据我所知,这开箱即用。我会检查你的路线......

答案 1 :(得分:1)

至少我会如何处理第二部分。如果我弄清楚第一个,我会回来更新。在“CreateNewVehiclePart”中,在提交按钮上方,呈现部分视图:

@using (Html.BeginForm("CreateNewVehiclePart", "Vehicle", FormMethod.Post, null))
{
    @Html.Partial("AddPart", new ShopLog.Models.Part())
    <input type="submit" value="Create New Vehicle Part" />
}

部分视图“AddPart”将是这样的:

@model ShopLog.Models.Part
@Html.EditorForModel()

然后在控制器中,“CreateNewVehiclePart”将处理帖子 - 只需将部件添加到数据库,然后重定向回原始视图。

编辑: 好的,这是我要为第一部分做的,也是部分视图。使用带有Vehicle ID的隐藏字段设置表单,以及部件的下拉列表:

@using (Html.BeginForm("AddPartsToVehicle", "Vehicle", FormMethod.Post, null))
(
    @Html.HiddenFor(model=>model.VehicleID)
    @Html.Partial("SelectPart", PartsList)
    <input type="submit" value="Add Parts To Vehicle" />
}

(请注意,“PartsList”是你必须在顶部的代码块中设置的东西 - 只是所有可用部分的列表。)然后部分视图“SelectPart”将是这样的:

@model IEnumerable<ShopLog.Models.Part>
@Html.DropDownList("part_id", Model.Select(m=>new SelectListItem() { Text = m.Name, Value = m.PartID.ToString() }))

然后,如前所述,您的“AddPartsToVehicle”将只处理这两个参数,并将ID为part_id的部分添加到车辆中。

显然我在这里留下了一些空白,但我认为这是一个不错的方法......

答案 2 :(得分:0)

我最终在此页面上使用了两个答案的组合。最终的问题是我在页面顶部有一个Save()按钮,它没有包含在自己的“@Using Html.BeginForm {}”中。 “TGH”帮助我确定了我的POST代码的实际位置。这就是我解决最终问题的原因。

使用部分表单确实帮助我使应用程序更简洁(页面重定向更少)。 “dbaseman”帮助我正确设置了这些设置。

感谢你们两位!