ASP.NET MVC动态地在Html.BeginForm提交之前保存数据

时间:2015-10-20 21:57:43

标签: javascript c# jquery asp.net-mvc razor

我确定之前已经回答过,但我认为我的搜索词汇让我失望了。我需要能够将数据添加到下面表单的元素中,特别是"命令","参数"和"安全"字段,在我提交整个表单之前。

@using (Html.BeginForm("CreateTemplateStep", "TemplateStep"))
{
<div>
    @Html.LabelFor(model => model.Name)
    @Html.EditorFor(model => model.Name)
</div>
<div>
    @Html.LabelFor(model => model.ExecutionOrder)
    @Html.EditorFor(model => model.ExecutionOrder)
</div>
<div>
    @Html.LabelFor(model => model.Description)
    @Html.EditorFor(model => model.Description)
</div>
<div>
    @Html.LabelFor(model => model.Type)
    @Html.DropDownListFor(model => model.Type, Enum.GetNames(typeof(Helion.JobScheduler.Models.StepType)).Select(e => new SelectListItem { Text = e }))
</div>    
<div>
    @Html.LabelFor(model => model.Commands)
    @Html.EditorFor(model => model.Commands)         
</div>
<div>
    @Html.LabelFor(model => model.Parameters)
    @Html.EditorFor(model => model.Parameters)
</div>
<div>
    @Html.LabelFor(model => model.Security)
    @Html.EditorFor(model => model.Security)
</div>

<button type="submit" class="btn btn-default btn-primary">Save</button>
<input type="button" value="Cancel" onclick="location.href='@Url.Action("Template","Template")';" class="btn btn-default" />
}

所以我需要添加尽可能多的命令,然后当我在表单上点击提交时,将提交整个命令列表。非常感谢任何帮助。

[DataModel("JOB", "TEMPLATE_STEP")]
public class TemplateStep
{
        public const string TABLE_NAME = "TEMPLATE_STEP";

        [PKIdentityDataColumn("TEMPLATE_STEP_ID")]
        public long? TemplateStepID { get; set; }
        [DataModelColumn("TEMPLATE_ID")]
        public long TemplateID { get; set; }
        [DataModelColumn("EXECUTION_ORDER")]
        public int ExecutionOrder { get; set; }
        [DataModelColumn("NAME")]
        public string Name { get; set; }
        [DataModelColumn("DESCRIPTION")]
        public string Description { get; set; }
        [DataModelColumn("TYPE")]
        public StepType Type { get; set; }
        [DataModelColumn("COMMAND_XML")]
        public string CommandXML { get; set; }
        [DataModelColumn("PARAMETER_XML")]
        public string ParameterXML { get; set; }
        [DataModelColumn("SECURITY_XML")]
        public string SecurityXML { get; set; }

        public List<NameValuePair> Commands { get; set; }
        public List<NameValuePair> Parameters { get; set; }
        public List<NameValuePair> Security { get; set; }        
}

这是关联的模型以及NameValuePair类。

public class NameValuePair
{
    public string Name { get; set; }
    public string Value { get; set; }
}

我们使用了自定义&#34; KeyValuePair&#34;因为我们无法让编辑器模板适用于实际的&#34; KeyValuePair&#34;结构

1 个答案:

答案 0 :(得分:0)

感谢@Stephen Muecke提供的链接和建议。我需要快速简单的东西,所以我的伴侣可以继续他正在做的事情。我所做的只是为我需要在视图中添加的每个字段添加几个按钮。

<div>        
    @Html.LabelFor(model => model.Commands)
    @Html.EditorFor(model => model.Commands)
    <input type="submit" name="command" value="addCom" class="btn btn-default btn-primary" />              
</div>
<div>
    @Html.LabelFor(model => model.Parameters)
    @Html.EditorFor(model => model.Parameters)
    <input type="submit" name="command" value="addParam" class="btn btn-default btn-primary" />
</div>
<div>
    @Html.LabelFor(model => model.Security)
    @Html.EditorFor(model => model.Security)
    <input type="submit" name="command" value="addSec" class="btn btn-default btn-primary" />
</div>

然后只是在我的控制器功能中添加了一些条件。

public ActionResult CreateTemplateStep(TemplateStep templateStep, string command)
    {
        if (command.Equals("addParam"))
        {
            templateStep.Parameters.Add(new NameValuePair());

            return View("CreateTemplateStep", templateStep);
        }
        else if (command.Equals("addCom"))
        {
            templateStep.Commands.Add(new NameValuePair());

            return View("CreateTemplateStep", templateStep);
        }
        else if (command.Equals("addSec"))
        {
            templateStep.Security.Add(new NameValuePair());

            return View("CreateTemplateStep", templateStep);
        }
        else if(command.Equals("saveStep")){ 
            Biz.SaveTemplateStep(templateStep);

            return RedirectToAction("EditTemplate", "Template", new {id = templateStep.TemplateID });
        }
        else
        {
            return View("CreateTemplateStep", templateStep);
        }
    }

就像我说快速而肮脏以保持球滚动。这将在未来变得更清洁,更有活力。随着事情的变化我会更新。任何进一步的建议或想法将不胜感激。