2012-05-05 7 views
1

아래 양식과 코드가 있습니다.Html.BeginForm()을 사용하여 요소의 ID를 얻는 방법

@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> 
} 

그리고 Html.BeginForm과 FormCollection를 사용하여 내가 중첩 된 span 태그의 ID를 얻으려면이

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

같은 QuestionController 내 삽입 작업입니다. 어떻게해야합니까? Plz 누군가 날 도와 줘. 고마워.

답변

3

제출 단추를 클릭하면 양식은이 양식에있는 모든 입력 값을 수집하고 다음 형식으로 서버에 보냅니다. inputId = inputValue. Span은 양식 내부의 입력 컨트롤이 아니며 양식은 서버에 보낼 값이나 다른 정보를 수집하지 않습니다. 숨겨진 입력 컨트롤을 생성하고 id 값을 설정할 수 있습니다. 그리고 액션의 서버 측에서 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(); 
} 

행운을 빈다.

+0

안녕하세요 @ 디마 슈미트, 귀하의 조언을 따르고 ** 실제로 작동합니다 **. 정말 고마워. :디 –

0

나는 할 수 없다고 확신합니다. 피들러를 사용하여 서버에 다시 게시되는지 확인할 수는 있지만 생각하지는 않습니다.

0

숨김 필드를 사용하여 스팬의 ID를 서버에 게시해야합니다. 보기가 강하게 입력 되었습니까?

관련 문제