2014-02-21 51 views
-3

我有一個隱藏字段在jQuery的功能沒有得到價值

@Html.HiddenFor(m => m.SchedulingProfileDetails.Id) 

,我試圖讓一個jQuery功能

var id = $('#SchedulingProfileDetails_Id').val(); 

隱藏字段的值,但我得到的價值爲空。爲什麼有人可以幫忙?

我生成的HTML

<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="SchedulingProfileDetails_Id" name="SchedulingProfileDetails.Id" type="hidden" value="" /> 
+6

檢查生成的html,看看它的id是什麼 –

+0

相同SchedulingProfileDetails_Id –

+0

在你的html中是否有? – Rickert

回答

0

確保您@Html.HiddenFor(m => m.SchedulingProfileDetails.Id)包含一些Id

另外我猜你是從控制器的方法傳遞該ID。如果不確定這樣做。

所以在控制器

public ActionResult foo() 
{ 
    var model = new Vm//this should be your model of the view 
    { 
     Id = //assign the value, and make sure it is not null, if it is you'll see the val you are mentioning as null 
     //assign some other stuff if you want 
    } 
    return View(Vm); 
} 

現在看來,您可以訪問的VAL:

var id = $('#@Html.IdFor(m=>m.SchedulingProfileDetails.Id)').val();//check the syntax i'm not sure if you need # or not,I haven't checked it on the code 

這應該工作。

有內置Html.IdFor(),將爲您提取id,並且您不必擔心正在生成的id。