views:

185

answers:

1

How submit list View in ASP.NET MVC. List View dont have

input type="submit" value="Save"

and I dont know where to put it.

Problematic  code is:



  <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcZezanje.Models.student1>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Studenti
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Studenti</h2>

    <table class="data-table">
        <tr>

            <th>
                Br. Indexa
            </th>
            <th>
                Prezime
            </th>
            <th>
                Ime
            </th>            
        </tr>

    <% foreach (var item in Model) { %>
            <tr>         
            <td>
                <%= Html.Encode(item.id_stud) %>
            </td>
            <td>
                <%= Html.Encode(item.prezime) %>
            </td>
            <td>
                <%= Html.Encode(item.ime) %>
            </td>           
        </tr>   
    <% } %>
    </table>
    <p>
        <%= Html.ActionLink("Create New", "Create") %>
    </p>
</asp:Content>
A: 

What do you want to submit? This code displays list of students. There is no user input. If there is no user input, there is no need to place submit button. if you want to edit user data, you'll have to do something like:

<form method="post" action="/Students/Save">

<table class="data-table">
    <tr>

        <th>
            Br. Indexa
        </th>
        <th>
            Prezime
        </th>
        <th>
            Ime
        </th>            
    </tr>

<% int i = 0; foreach (var item in Model) { %>
        <tr>         
        <td>
            <%= Html.TextBox("students[" + i + "].id_stud", item.id_stud) %>
        </td>
        <td>
            <%= Html.TextBox("students[" + i + "].prezime", item.prezime) %>
        </td>
        <td>
            <%= Html.TextBox("students[" + i + "].ime", item.ime) %>
        </td>           
    </tr>   
<% i++;} %>
</table>


<input type="submit" />
</form>

Here you can read about binding to list:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

It has changed a little, you don't have to specify index anymore.

LukLed
I forgot to add checkBox in all rows. User needs to check any of students and that is users input for submit.
Ognjen
OK, that makes sense. You can add Html.Checkbox for every row and Html.Hidden for identification.
LukLed
That is no problem.Thank you for answer.Do you know way how quickly learn repository pattern in ASP.NET MVC. Now I dont use it but I have a lot of linq queries that are repeated with small modifications. Is repository pattern solutions for me???
Ognjen
Do you use Linq to SQL? If yes, you should read this question: http://stackoverflow.com/questions/125453/implementation-example-for-repository-pattern-with-linq-to-sql-and-c
LukLed
yes,I use Linq to SQL
Ognjen