如何修复冲突的'Index()'ActionResults

时间:2016-05-20 06:19:52

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

在我的视图(Index.cshtml)中,我有一个复选框列表和一个文件输入表单,两者都无法协同工作,因为它们都使用import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JButton; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; import javax.swing.table.TableModel; import javax.swing.JTextArea; import javax.swing.JTable; import javax.swing.JTextPane; import javax.swing.border.BevelBorder; public class TBB_SQLBuilder { private JFrame frame; private JTable table; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { TBB_SQLBuilder window = new TBB_SQLBuilder(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public TBB_SQLBuilder() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 950, 900); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextArea textArea = new JTextArea(); JButton button = new JButton("New button"); table = new JTable(); table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); GroupLayout groupLayout = new GroupLayout(frame.getContentPane()); groupLayout.setHorizontalGroup( groupLayout.createParallelGroup(Alignment.TRAILING) .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup() .addContainerGap() .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) .addComponent(table, GroupLayout.DEFAULT_SIZE, 938, Short.MAX_VALUE) .addGroup(groupLayout.createSequentialGroup() .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 289, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(button))) .addContainerGap()) ); groupLayout.setVerticalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup() .addContainerGap() .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE) .addComponent(button)) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(table, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE) .addContainerGap(613, Short.MAX_VALUE)) ); frame.getContentPane().setLayout(groupLayout); frame.add(new JScrollPane(table)); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ String getValue = textArea.getText(); String connectDB = "jdbc:ucanaccess:///Users/sebastianzeki/Documents/PhysJava/My.mdb;"; Connection conn; try { conn = DriverManager.getConnection(connectDB); Statement st =conn.createStatement(); ResultSet rsHNum = st.executeQuery(getValue); table.setModel(buildTableModel(rsHNum)); ((DefaultTableModel)table.getModel()).fireTableDataChanged(); // show changes } catch (SQLException e) { e.printStackTrace(); } } }); } public static DefaultTableModel buildTableModel(ResultSet rs1) throws SQLException { ResultSetMetaData metaData = rs1.getMetaData(); // names of columns Vector<String> columnNames = new Vector<String>(); int columnCount = metaData.getColumnCount(); for (int column = 1; column <= columnCount; column++) { columnNames.add(metaData.getColumnName(column)); } // data of the table Vector<Vector<Object>> data = new Vector<Vector<Object>>(); while (rs1.next()) { Vector<Object> vector = new Vector<Object>(); for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) { vector.add(rs1.getObject(columnIndex)); System.out.println(rs1.getObject(columnIndex)); } data.add(vector); } return new DefaultTableModel(data, columnNames); } 而你在控制器中不能有多个Index(),所以我该怎么办?创建另一个视图?我不想创建另一个视图,因为我想在同一个视图上输入我的复选框和文件,所以该怎么办?

复选框:

Index()

文件输入表格:

[HttpGet]
public ActionResult Index() //Index being used
{
    var list = new List<Album>
    {
        new Album { Id = 1, Name = "Aquafina", Checked = false },
        new Album { Id = 2, Name = "Mulshi Springs", Checked = false },
        new Album { Id = 3, Name = "Alfa Blue", Checked = false },
        new Album { Id = 4, Name = "Atlas Premium", Checked = false },
        new Album { Id = 5, Name = "Bailley", Checked = false },
        new Album { Id = 6, Name = "Bisleri", Checked = false },
        new Album { Id = 7, Name = "Himalayan", Checked = false },
        new Album { Id = 8, Name = "Cool Valley", Checked = false },
        new Album { Id = 9, Name = "Dew Drops", Checked = false },
        new Album { Id = 10, Name = "Dislaren", Checked = false },
    };
    return View("Index", list);
}

[HttpPost]
public ActionResult Index(List<Album> list) //2nd Index being used, so far so good
{
    var selected = list.Where(x => x.Checked).Select(x => x.Name);
    //ViewBag.Values = String.Join(", ", list);
    ViewBag.Values = selected;

    return this.View("Index", list);
}

4 个答案:

答案 0 :(得分:2)

在视图中定义表单操作到文件上传,此操作的名称取决于您。

Index.cshtml

@Html.BeginForm("UploadFile", "Controller", new { type = "form/multipart" })
{
    <input type="file" name="file" />
    <button type="submit" value="Upload" />
}

@Html.BeginForm("Index", "Controller")
{
    <input type="checkbox" name="Album" value="No. Dolls!" />
    <input type="checkbox" name="Album" value="O.B.I." />
    <button type="submit" value="Upload" />
}

和Controller.cs

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file) 
{ 
    // here you are working with uploaded file
    return View("Index"); 
}

[HttpPost]
public ActionResult Index(List<Album> list)
{
    // here you are working with checkbox list items
    return View();
}

主题说明:使用接口类instate具体实现List<Album> =&gt; IList<Album>; IList可以添加相册,将相册添加到上传的集合中是否有意义?

如果没有,我建议使用IEnumerable<Album>,但IEnumerable存在问题:您可以修改元素,代码的某些部分不会发生变化(例如设置空值)它可以导致Null引用异常。如果您对此感到满意,请使用它。

如果没有,我的建议是使用IReadOnlyCollection

答案 1 :(得分:1)

您可以使用ActionName属性为第二个Index方法设置索引别名,然后重命名该方法。

答案 2 :(得分:0)

  1. 第一个索引有效,因为它是[HttpGet]
  2. 第二个索引有效,因为它是[HttpPost]
  3. 您无法在该控制器中定义另一个名为Index for HttpGet或HttpPost的操作,因为当您调用该操作时,MVC将如何知道要调用哪个索引?

    只需给出其他两个动作的唯一名称:

    public ActionResult BindCombobox()
    {
    
    }
    
    [HttpPost]
    public ActionResult UploadFiles(HttpPostedFileBase file)
    {
    
    }
    

答案 3 :(得分:0)

与Ton Plooij建议一样,使用ActionName并为控制器功能使用不同的方法名。

[HttpGet, ActionName("Index")]
public ActionResult GetIndex()
{
    ...
}

[HttpPost, ActionName("Index")]
public ActionResult PostIndex()
{
    ...
}