0

have been struggling with this. Tried everything I can think of. Im using javascript to pass data to db, works fine with ints on another page but now with strings it wont work :s

 @using (Html.BeginForm(null, null, FormMethod.Post, new{@id="manageForm"}))
    {
    @Html.AntiForgeryToken()

    <span class="actions">

     @T(User.Id.ToString()) @T("  ") @T(ViewData["Tag"].ToString())

<input type="hidden" name="tag" value="fr" />
    <input type="hidden" name="id" value="3" />
<a href="#"class="bttn green large"  onclick="return following.();">@T("Follow")</a>


</span>
}

Javascript

<script type="text/javascript">
    function followTag() {
        $('#manageForm').attr('action', '@(Url.Action("FollowTag"))').submit();
        return false;
    }
    </script>

Controller

[RequireAuthorization]
        [HttpPost]
        public ActionResult FollowTag(int id, string tag)
        {
            _service.FollowTag(id, tag);
            return RedirectToAction("TagPage","Detail", new
            {
            });
        }

Data Access

   public void FollowTag(int id, string tag)
    {
        DbCommand comm = GetCommand("SPTagFollow");
        //user id
        comm.AddParameter<int>(this.Factory, "id", id);
        //id to follow
        comm.AddParameter<string>(this.Factory, "tag", tag);

        comm.SafeExecuteNonQuery();
    }

route is setup fine and sql(stored procedure) executes perfect. Hopefully one of you can see something obvious

cheers

mxadam
  • 169
  • 2
  • 14
  • Not sure how to manage it with Razor really, but typically if I want to upload to a database with javascript, I use ajax. This is for a php implementation, but the javascript is the same regardless. It just passes form values to the server code for upload. http://stackoverflow.com/questions/15107453/uploading-file-to-php-through-ajax` – Ortund Mar 11 '13 at 09:15
  • cheers ortund. I am going to use ajax later on. Haven't ever used ajax so was going to do this to get everything in place then fiddle with ajax. Maybe I should just have a look now :P cheers – mxadam Mar 11 '13 at 23:26

1 Answers1

0

I think is a problem of mistyping, check your last <a> tag, you typed following.() in the onclick event, see that your javascript function is called followTag.

If that doesn't fix it, then get rid of that foolowTag function, you can specify the action and the controller in the form itself, like this:

@using (Html.BeginForm("FollowTag", "YourControllerName", FormMethod.Post)) {
    ...
    //Delete this line
    //<a href="#"class="bttn green large"  onclick="return following.();">@T("Follow")</a>
    //This submit button will do the job
    <input type='submit' value='@T("Follow")' />
}

That should do it. If you are using the anchor tag just for styling that's ok, otherwise you should use the other way, I think is clearer and besides it takes advantage of razor's great features.

educampver
  • 2,967
  • 2
  • 23
  • 34
  • thanks for this, very helpfull. Wish I could give this as the answer but doesnt get code working, very strange as its passing the values, and then returning to page but nothing in db, very frustrating. Thanks tho for this :) – mxadam Mar 11 '13 at 23:24
  • hey if u can help is this route right? calling from tags/sample but really tags/{tag} is the route – mxadam Mar 11 '13 at 23:49
  • with tag as a constraint – mxadam Mar 11 '13 at 23:50
  • hours later and its finally fixed. I was calling users controller from within a view of forums controller, Had to transfer it all over. Is this not possible? meaning if i wanted users to be able to follow a tag i would have to add this whole thing into every controller. What a mess that would be. Can you not call this sort of thing from outside the controller the action is in (as in my case this is calling action inside users controller but from forums) – mxadam Mar 12 '13 at 02:36
  • i mean users to be able to follow a tag from any view eg through popups etc – mxadam Mar 12 '13 at 02:51
  • @user1311546 if I'm getting you right, you can indeed do what you want with exactly the same code I posted in my answer, you can create a `form` tag in any view you want specifying in the `@Html.BeginForm` the action and the controller you want to be passed that data, in this case it would be something like this: `@using (Html.BeginForm("FollowTag", "Users", FormMethod.Post))` – educampver Mar 12 '13 at 13:08
  • thanks ecampver. Thought it would but wasnt working before i moved it to the same controller as the view, weird huh. anyway cheers ill give ya the answer tick :) – mxadam Mar 12 '13 at 23:45
  • Just thought I would update this. If anyone is wanting to make a twitter style following system its better to use ajax.beginform to update partial views and data – mxadam Mar 24 '13 at 01:13