0

im using the sortable jquery plugin, because i want that the user can choice the order of the images are showing in my view.

For this i have a Get Controller which sends the data to my PartialView.

How can i make the now the Post Controller to update my table in my database?

Note: In this moment the controller don´t receive any data. i haven´t figured out what is wrong

Someone can help me with this?

Thanks in advance:

Here is my code:

In My PartialView:

@(Html.BeginForm("UpdateOrder", "Admin", FormMethod.Post)){
            <div id="Order">
                <ul id="sortable">

                    @foreach (var p in ViewBag.Images)
                    {           
                        <li id="@Html.AttributeEncode(p.FileName)">
                            <img src="~/Files/@p.FileName"/>
                        </li>
                     }
                </ul>
            </div>
         }

Controller:

if (ModelState.IsValid)
{
    using (SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    {
         SqlCommand cmd;
         System.Text.StringBuilder sql = new System.Text.StringBuilder();
         sql.Append("Update Image Set MyFileName=??? Order By ASC");

         cn.Open();
         cmd = new SqlCommand(sql.ToString(), cn);
         cmd.Parameters.Add(??????????).Value = ;

         cmd.ExecuteNonQuery();

         cn.Close();
     }
}      
return View();
Tobias
  • 232
  • 1
  • 16
user2232273
  • 4,898
  • 15
  • 49
  • 75

1 Answers1

0

Are you looking for something like this?

SqlCommand comm = new SqlCommand("UPDATE Customers SET Name=@name WHERE ID=@id", con;
comm.Parameters.AddWithValue("@name", "John");
comm.Parameters.AddWithValue("@id", id);

For passing data from view to controller take a look at the following links:
ASP.NET MVC 3 Razor: Passing Data from View to Controller
ASP.Net MVC Passing multiple parameters to a view

I have created a backend for a website, where i e.g. can add, edit, delete events. So the snippet in the view for the events looks like this:

<div id="tabs-2" class="ui-widget-content">
    <h2>
        Events</h2>
    <p>
        @Html.ActionLink("Create new Event", "EventCreate")
    </p>
    <table id="tableEvent">
        <tr>
            <th>
                Event
            </th>
            <th>
                Date
            </th>
            <th>
                Day of the Week
            </th>
            <th>
            </th>
        </tr>
        @foreach (var e in ViewBag.Events)
        {
            <tr id="@e.EventID">
                <td>
                    <h4>@e.EventName</h4>
                </td>
                <td>
                    <h4>@string.Format("{0:d}", e.Datum)</h4>
                </td>
                <td>
                    <h4>@e.Tag</h4>
                </td>
                <td>
                    @Html.ActionLink("Löschen", "EventDelete", new { id = e.EventID })
                </td>
            </tr>
        }
    </table>
</div>

I pass the id to the controller in the ActionLink and call the EventDelete:

[Authorize]
public ActionResult EventDelete(int id)
{
    repevent.Delete(id);
    return RedirectToAction("Main");
}

Now i have the id of the event and can do whatever i want. (In my case, i delete the event with the associated id.

public void Delete(int id)
{
    using (KiGaDBEntities db = new KiGaDBEntities())
    {
        Event event = db.Event.SingleOrDefault(e => e.EventID == id);
        if (event != null)
        {
            db.Event.Remove(event);
            db.SaveChanges();
        }
    }
}

I hope that helps you!

Community
  • 1
  • 1
Tobias
  • 232
  • 1
  • 16
  • the problem i have is that the controller don´t receive any data – user2232273 Sep 04 '13 at 13:54
  • sorry i can't tell you exactly how it works off-hand now, but i think i have an example like this at home on my laptop and i can take a look on it and try to help you with your question. – Tobias Sep 04 '13 at 13:58
  • meanwhile you could take a look at the link i've edited in my answer, perhaps it helps you! – Tobias Sep 04 '13 at 14:03