Question

I have the form and some code below.

@using (Html.BeginForm("Insert", "Question", "POST"))
{
    <div id="add_tag">
        <div id="list_tag">
            <span class="post-tag" id="2">Phi kim<span class="delete-tag" title="Xóa Tag này"></span></span>
            <span class="post-tag" id="22">Hóa Vô Cơ<span class="delete-tag" title="Xóa Tag này"></span></span>
            <span class="post-tag" id="1">Lý<span class="delete-tag" title="Xóa Tag này"></span></span>
        </div>
        <div class="tag-suggestions hidden">
        </div>
    </div>
    <div class="form-sumit clear">
        <input type="submit" id="submit-button" value="Post your question" />
    </div>
}

And my Insert action in QuestionController like this

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Insert(FormCollection _form)
    {
        //my code here
    }

I want to get id of span tag nested in by using Html.BeginForm and FormCollection. How can I do that? Plz someone help me. Thanks a lot.

Was it helpful?

Solution

When you click on submit button, form collects all input values inside this form and send to the server with the following format: inputId=inputValue. Span isn't the input control inside the form and form does not collect its value or another information to send to the server. You can generate hidden input control and set the id value to it. And then at the server side in the action you can get it from FormCollection.

[HttpPost]
[ValidateInput(false)]
public ActionResult Insert(FormCollection formCollection)
{
    //for example all hidden input controls start with "hidden_tag" id 
    //and end with your number of tag:
    var allNeededKeys = formCollection.AllKeys.Where(x => x.StartsWith("hidden_tag"));
    var listOfId = allNeededKeys.Select(formCollection.Get).ToList();
}

Good luck.

OTHER TIPS

I'm pretty sure you can't. You can use fiddler to see if they're posted back to the server but I don't think they are.

You should use hidden fields to post the span's id to the server. Is the view strongly typed?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top