2015-10-20 70 views
0

之前,我敢肯定,这已经回答过了,但我觉得我的搜索词汇是扔我。在提交整个表单之前,我需要能够将数据添加到下面表单的元素,特别是“命令”,“参数”和“安全性”字段。ASP.NET MVC dynamicaly数据保存Html.BeginForm提交

@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; } 
} 

我们使用了自定义“KeyValuePair”,因为我们不能让编辑模板,以便为实际的“KeyValuePair”结构的工作。

+0

不清楚你的要求。你想提交一个'Commands'的集合吗?什么是命令类型?你需要展示你的模型 –

+0

你也可以分享模型吗?需要看到的类型 – LiranBo

+0

你是否想为'Commands'等动态添加和删除新的'NameValuePair'项,或者只是编辑现有的项? –

回答

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); 
     } 
    } 

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