如何在同一个asp.net mvc表单上使用2个按钮

时间:2015-04-06 22:55:39

标签: c# asp.net asp.net-mvc asp.net-mvc-4

在我的asp.net mvc表单中,我有2个按钮,一个用于保存,它将保存sharepoint中列表中的from数据,第二个按钮执行相同操作,此外它还应用了一些css颜色。

我怀疑如何在同一表格上使用2个动作(同一个控制器) 这是我的看法

   @{
    Layout = "~/Views/Shared/_LayoutPage2.cshtml";
}
@using (Html.BeginForm("Index", "Movies", FormMethod.Post))
{
<div class="row">
    <div class="col-md-8">
        <div class="col-xs-6 col-sm-3" id="stylesheet">Hojas de estilos</div>
        <div class="col-xs-6 col-sm-3">
            @Html.DropDownList("cssFiles", (IEnumerable<SelectListItem>)ViewBag.cssFiles, "Crear Nuevo", new { @class = "form-control", @id = "selCssFile" })
            <span>
                <input type="text" class="form-control" id="txtFileName" style="display:none;" placeholder="Nombre del archivo">
            </span>
        </div>
    </div>
    </div>
<div class="row">
    <div class="col-md-8">
        Color base, links, botones, borde encabezado y pie
    </div>
    <div class="col-md-4">
        <div id="colorSelector" class="colorSelector"><div style="background-color: #0000ff"></div></div>
    </div>
</div>
<div class="row">
    <div class="col-md-8">
        Fondo de la pagina, fondo de los cuadros
    </div>
    <div class="col-md-4">
        <div id="colorSelector2" class="colorSelector"><div style="background-color: #0000ff"></div></div>
    </div>
</div>
<div class="row">
    <div class="col-md-8">
        Navegación
    </div>
    <div class="col-md-4">
        <div id="colorSelector3" class="colorSelector"><div style="background-color: #0000ff"></div></div>
    </div>
</div>
<div class="row">
    <div class="col-md-8">
        Navegación (Item seleccionado)
    </div>
    <div class="col-md-4">
        <div id="colorSelector4" class="colorSelector"><div style="background-color: #0000ff"></div></div>
    </div>
</div>
<div class="row">
    <div class="col-md-8">
        Pie de página
    </div>
    <div class="col-md-4">
        <div id="colorSelector5" class="colorSelector"><div style="background-color: #0000ff"></div></div>
    </div>
</div>
<div class="row" id="buttons">
    <div class="col-md-8">
    </div>
    <div class="col-md-4">
        <button type="button" class="btn btn-success">Guardar</button>
        <button type="button" class="btn btn-primary">Guardar y aplicar</button>
    </div>
</div>
}

到目前为止我在自定义控制器上的索引操作

 public class CustomizeController : Controller
    {
        // GET: Customize
        public ActionResult Index()
        {
            User spUser = null;

            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            using (var cc = spContext.CreateUserClientContextForSPHost())
            {
                int aprovisionado = (int)cc.Web.GetPropertyBagValueInt("Vinculosc.PlantillasIntranet.Aprovisionado", 0);
                if (aprovisionado == 0)
                {
                    string libraryName = "ConfiguraciónColores";
                    Dictionary<string, string> fields = new Dictionary<string, string>();
                    fields.Add("Color1", "Text");
                    fields.Add("Color2", "Text");
                    fields.Add("Color3", "Text");
                    fields.Add("Color4", "Text");
                    fields.Add("Color5", "Text");
                    //ProvisionTemplate(cc);
                    CreateLibrary(cc, libraryName);
                    AddFields(cc, libraryName, fields);
                }
            }
            #region comments
            /*Uri hostWeb = new Uri(Request.QueryString["SPHostURL"]);

            using (var clientContext = TokenHelper.GetS2SClientContextWithWindowsIdentity(hostWeb, Request.LogonUserIdentity)) 
            {
                var web = clientContext.Web;

                clientContext.Load(web, w => w.Lists.Include(l => l.Title).Where(l => !l.hidden));
                clientContext.ExecuteQuery();

                return View(web.Lists);
            }*/
            #endregion

            return View();
        }

2 个答案:

答案 0 :(得分:1)

你的按钮类型应该是“提交”,你可以给它们一个名字......同名,可以在模型中重用,或者通过Request.Form [“GiveAName”]

您的控制器应该有

[HttpPost]
public ActionResult Index()
{
... Your code to retrieve form values
}

无论如何,编码错误...您应该使用模型注入视图,可以检索相同的模型,因此您不必担心检索表单值。 :=)

答案 1 :(得分:1)

您可以在视图中使用不同的值

添加相同的名称
<button type="submit" name="Guardar" value="guardar" class="btn btn-success">Guardar</button>
<button type="submit" name="Guardar" value="aplicar" class="btn btn-primary">Guardar y aplicar</button>

在Controller中,您可以检查按钮的值

if (Request["Guardar"].ToString() == "guardar")
{
    //Your code for the first button
}
else
{
    //Your code for the second button
}