locked
How to update multiple tags RRS feed

  • Question

  • User-1595131707 posted

    I've the following code, which needs to get tags and update them. Unfortunately, it'll only update if there is only one tag. But, when there are two or more tags, update doesn't work. Any idea?

    <form action="" method="post">
    @foreach(var tag in result){
    
        <input type="hidden" name="tagID" value="@tag.Id"  />
        <input type="text" name="@tag.Id" class="form-control input-lg" value="@tag.TagName"  />
    
    }
    </form>
    
    
    @if (IsPost)
     {
            var TagN = Request.Unvalidated().Form["@tag.Id"];
            var TagI = Request["tagID"].AsInt();
    
            var sql = "UPDATE Tag_New SET TagName=@0 WHERE Id=@1";
            db.Execute(sql, TagN, TagI);
    
            Response.Redirect("~/posts.cshtml");
      }
    

    Wednesday, February 4, 2015 1:11 PM

Answers

All replies

  • User-821857111 posted

    You need an IN clause in your SQL, which requires a helper: http://www.mikesdotnetting.com/article/156/webmatrix-database-helpers-for-in-clauses

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2015 2:28 PM
  • User-1595131707 posted

    Mike,

    I tried to look into the article about the IN clause in SQL but I couldn't get my problem resolved. Any suggestion?

    Thanks,

    Friday, February 20, 2015 11:39 PM
  • User-821857111 posted

    What did you try? 

    Saturday, February 21, 2015 2:42 AM
  • User-1595131707 posted

    As per the instruction in your article, I've created the helper class and put it into the App_Code folder. Below is the code:

    <form action="" method="post">
    @foreach(var tag in result){
    
        <input type="hidden" name="tagID" value="@tag.Id"  />
        <input type="text" name="@tag.Id" class="form-control input-lg" value="@tag.TagName"  />
    
    }
    </form>
    
    
    @if (IsPost)
     {
            var TagN = Request.Unvalidated().Form["@tag.Id"];
            var temp = Request["tagID"].Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries);
    	var parms = temp.Select((s, i) => "@" + i.ToString()).ToArray();
    
            var sql = "UPDATE Tag_New SET TagName=@0 WHERE Id IN ({@1})";
            db.Execute(sql, TagN, temp);
    
            Response.Redirect("~/posts.cshtml");
      }

    Anything I miss?

    Saturday, February 21, 2015 7:42 AM
  • User-821857111 posted

    I think you need to take another look at the article. You say you have added the helper to your App_Code folder, but the code you showed doesn't seem to use it at all. 

    Saturday, February 21, 2015 11:49 AM