开发者

Submit Data to a datatable from a View MVC

开发者 https://www.devze.com 2022-12-25 03:27 出处:网络
I have a view that I would like to populate data when the next button is clicked.It is 3 Views which will send data on every next button.How do I do this?

I have a view that I would like to populate data when the next button is clicked. It is 3 Views which will send data on every next button. How do I do this?

Below is code I just made up, but should give an idea of what I am looking for...

Page 1:

    <table>
    <tr>
        <td><b>Name:</b></td>
        <td colspan="2"><input id="txtName" type="text" /></td>                        
    </tr>
    </table>
    <input  type="submit" value="Next" />

Page 2:

    <table>
    <tr>
        <td><b>Address:</b></td>
        <td colspan="2"><input id="txtAddress" type="text" /></td>                        
    </tr>
    </table>
    <input  type="submit" value="Next" />

Page 3:

    <table>
    <tr>
        <td><b>Phone:</b></td>
        <td colspan="2"><input id="txtPhone" type="text" /></td>                        
    </tr>
    </table>
    <input  type="submit" value="Next" />
开发者_运维百科


In your view, you need a form. The action is your action on the particular controller you want to call. On submit the action gets a formcollection on post and this is where your data from your form will be. Take the data and create your entity for the database object type. Then insert the object into the database. Assuming you will implement Linq to Sql or something similiar.

MyView

 <% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
 <table>
    <tr>
        <td><b>Name:</b></td>
        <td colspan="2"><input id="txtName" type="text" /></td>   
    </tr>
 </table>
 <input  type="submit" value="Next" />
 <% Html.EndForm(); %>

MyController

 [AcceptVerbs(HttpVerbs.Post)]
 public virtual ActionResult MyAction(FormCollection collection)
 {
    // take your data from your collection and populate entities to save into your db
    return RedirectToAction("Page2"); // or whatever the action is you want to redirect to
 }


I figured it out. I used the name attribute on the input then store in the object before I use a stored procedure to insert the values.

<td colspan="2"><input name="Name" id="txtName" type="text" /></td>

MODEL

public string Name {get; set;}
//Constructor
using (connection)
{
    SqlCommand command = new SqlCommand("StoredProcName", connection);
    command.Parameters.Add(new SqlParameter("name", Name));
    connection.Open();
    command.ExecuteNonQuery();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号