Spam Master
Spam Master

Reputation: 13

Issue with editing data and model binding

I started working with asp.net and I have encountered a problem when I try to edit multiple values from a table. I have a bookmark tables which is connected to another tag table, with an 1 : N relationship. My problem is when I want to edit already existing tags associated with an existing url. I can display them on the page but when I try to post the edited data I don't know how to pick it up in the controller. So far I have managed to send them back as a string but I doubt that is the solution since I have to edit all the data again later. I want to replace the existing values in the Tag table with the edited data. Here are my model and controller code snippets.

Bookmark model:

public int id { get; set; }
public string url { get; set; }

public virtual ICollection<Tag> tags { get; set; }

Tag model:

public int id { get; set; }

public string name { get; set; }

public virtual Bookmark bookmark { get; set; }

public string user { get; set; }

Controller:

public ActionResult Edit(int id)
{
    var editBookmark = adc.Bookmarks.Single(x => x.id == id);
    var query_where2 = from a in adc.Tags
                       where a.bookmark.id == id
                       select a;
    BookmarkTag bkTag = new BookmarkTag();

    bkTag.bookmark = new List<Bookmark>();
    bkTag.bookmark.Add(editBookmark);
    bkTag.tag = query_where2.ToList();

    return View(bkTag.tag);
}

//
// POST: /SavedBookmark/Edit/5
[HttpPost]
public ActionResult Edit(int id, ICollection<FormCollection> tag)
{
    try
    {
        return View();
    }
    catch
    {
        return View();
    }

Html code:

@using (Html.BeginForm("edit", "SavedBookmark"))
{
    @Html.AntiForgeryToken()
    if (Model != null) {
        var aa= Model.First();
        @Html.TextBox("test2", aa.bookmark.url);

        List<BookIT2.Models.Tag> allTags = new List<BookIT2.Models.Tag>();
        allTags = Model.ToList();
        for (int i = 0; i < allTags.Count; i++)
        {

            if (!allTags[i].name.IsEmpty())
            {
                @Html.TextBox(allTags[i].name, allTags[i].name);
                @Html.Hidden(allTags[i].id.ToString(), allTags[i].id);
                @Html.Hidden(allTags[i].user, allTags[i].user)
                @Html.Hidden(allTags[i].bookmark.id.ToString(), allTags[i].bookmark.id.ToString())
            }
        }


    @Html.Label("Additional tag")
    @Html.TextBox("additionalTag")
    <input type="submit" value="edit" />
}

In short: I can't get any values in the http post ICollection, it's always null.

Here is the updated code:

@using (Html.BeginForm("edit", "SavedBookmark"))
{
    @Html.AntiForgeryToken()
    if (Model != null)
    {

        for (int i = 0; i < Model.tag.Count; i++)
        {
            if (!Model.tag[i].name.IsEmpty()) { 
            @Html.Hidden(Model.tag[i].id.ToString(), Model.tag[i].id);
            @Html.Label("name");
            @Html.TextBox(Model.tag[i].name, Model.tag[i].name);
            @Html.Hidden(Model.tag[i].bookmark.id.ToString(), Model.tag[i].bookmark.id);
            @Html.Hidden(Model.tag[i].user, Model.tag[i].user);
}
        }
        @Html.TextBox(Model.bookmark.id.ToString(), Model.bookmark.url);

        <input type="submit" value="edit" />
    }
}

Model class:

public class TestBookmark
{
    public Bookmark bookmark{get; set;}

    public List<Tag> tag {get; set;}
}

[HttpPost]
public ActionResult Edit(TestBookmark edit)
{}

Upvotes: 0

Views: 82

Answers (1)

Eru
Eru

Reputation: 397

Don't really understand why you're doing it this way. I would like to suggest you totally different approach. First:
Create a class with all the fields you want in your view.
Second:
Use this class as the MODEL in your View
Third:
In the controller, in the POST function user your class as the only one parameter of that function.

Upvotes: 0

Related Questions