2016-07-05 12 views
0

我在Asp.net MVC將數據綁定的跨度與ASP MVC的數據庫,使用模型綁定

我需要怎麼做,然後反給了它的價值模型綁定一些建議很新。

我有兩個modal Pages.in第一個我有這個計數器供用戶選擇他們的要求的數量是這樣的:在 當用戶點擊加號圖標數改爲By Javascript

enter image description here

當用戶點擊下一個按鈕,下一個模態頁面顯示,其中我有selecteddatenumber of travelers那裏的JavaScript。也是我想要模型綁定器作爲它的一個對象的形式。

這裏是我的下一個模式頁的概要

<p> 
<span id="selecteddate"></span> 


    <span id="selectedcount"></span> 


    <span id="selectedprice"></span> 
</p> 

的觀點,這裏是JavaScript的:

$(".nextbtn").click(function() { 

$("#selecteddate").html("Selected Date is : "+$("#showdate").html()); 
$("#selectedprice").html("Total Price is : " + $("#tpriceforall").html()); 
$("#selectedcount").html($("#shownum").html()); 
}); 

和它工作正常。

而且我也在這個下一個模態頁面中有一個窗體。我想將用戶的輸入數據傳遞給數據庫+ span#selectedcount中的可用數字java script set it each time

因爲我想使用ModelBinder,有一個輸入它將被隱藏。 但只是爲了傳遞數據。這裏是我的模型請求TravelersCount輸入

<div class="form-group"> 
@Html.LabelFor(model => model.Request.TravelersCount, htmlAttributes: new {@class = "control-label col-md-2" }) 
<div class="col-md-10"> 
    @Html.EditorFor(model => model.Request.TravelersCount, new { htmlAttributes = new { @class = "form-control", @*@Value=?????*@ } }) 
@Html.ValidationMessageFor(model => model.Request.TravelersCount, "", new { @class = "text-danger" }) 
</div> 
</div> 

我應該寫什麼在EditorForValue

或者,如果這種方式是完全不規則的,請建議我一個新的。 真的很感謝你的幫助。謝謝

+0

您的'EditorFor()'方法創建一個輸入'id =「Request_TravelersCount」'所以你只需要'$('#Request_TravelersCount').val($(「#selectedcount」)。text ));'設置它的值(並且從不嘗試在'HtmlHelper'方法中設置'value'屬性) –

+0

@StephenMuecke哦,我的上帝它的作品:)真的很感謝你。你有可能回答這個問題嗎? –

回答

1

您的EditorFor()方法創建一個輸入與id="Request_TravelersCount"

更新其value單擊該按鈕時,您可以使用

$(".nextbtn").click(function() { 
    $("#selecteddate").html("Selected Date is : "+$("#showdate").html()); 
    $("#selectedprice").html("Total Price is : " + $("#tpriceforall").html()); 
    var num = $("#shownum").html(); 
    $("#selectedcount").html(num); 
    $('#Request_TravelersCount').val(num); 
}); 

旁註:你不應該嘗試設置值屬性的HtmlHelper方法(該方法將設置正確的值考慮到帳戶ModelState值)

+0

html方法的設置值在每個地方都不適用? –

+0

不,你永遠不應該那樣做。 HtmlHelper方法會正確地考慮屬性的值和/或ModelState或ViewDataDictionary中的任何值(如果它們存在的話)。 –

+0

真的非常感謝你1000000次Stephen。 –