2013-10-11 43 views
0

我在視圖中有4個文本框,並且需要將我從數據庫中的數據庫中獲取的值分配在一起,這是因爲我的表結構。現在我面臨的問題賦值到控件中查看和使用相同的controls.Please幫助更新同一個數據庫,使用具有變量值的模型分配文本框

public ActionResult Index() 
{ 
    // SettingsModel smodel = new SettingsModel(); 
    // tblSettings tableset = new tblSettings(); 

    var dat = _Settings.GetSettings().ToDictionary(s => s.Desc, s => s.Settings, StringComparer.Ordinal); 
    return View(); 
} 

// DAT我從tablestructure.I所有值figureouthow不可能 將這些值應用於我在視圖中使用模型的文本框中。作爲 我需要使用文本框

表結構更新相同的表: ID(INT),設置(nvarchar的),說明(NVARCHAR)

enter image description here

UPDATE:

@(Html.Kendo().TimePicker() 
    .Name("startpicker") 
    .Interval(60) 
//.Value("10:00 AM") 
    ) 
@(Html.Kendo().TimePicker() 
    .Name("endpicker") 
    .Interval(60) 
//.Value("10:00 AM") 
    ) 
<td>@Html.TextBoxFor(Model => Model.DefaultState, new { @class = "k-textbox", style = "width: 118px;", id = "statetxt" }) </td> 

Thankyou回覆。我得到的分配除了tooltipcheckbox和劍道timepicker所有值:

這是我在控制器代碼:

settingsmodel smodel=new settingsmodel(); 
     if (dat.ContainsKey("Tooltips")) 
     smodel.tooltip =Convert.ToBoolean(dat["Tooltips"]); 

//我在工具提示 得到的值爲0 //它拋出錯誤的字符串是未被識別爲有效的布爾

settingsmodel:

public bool tooltip { get; set; } 
+0

你在哪裏從控制器傳遞數據到視圖?請發佈您的查看代碼。 – ckv

回答

1

您應該將模型傳遞到視圖中。

public ActionResult Index() 
{ 
    // SettingsModel smodel = new SettingsModel(); 
    // tblSettings tableset = new tblSettings(); 

    var dat = _Settings.GetSettings().ToDictionary(s => s.Desc, s => s.Settings, StringComparer.Ordinal); 
    return View(dat); //dat is your model. 
} 

而在視圖內部,您可以從模型中獲取數據。

你的模型看起來像一本字典做你的視點側將類似於以下內容..

@foreach (KeyValuePair<string, string> item in ((IDictionary<string, string>) Model)) 
{ 
    <input type="text">@item.Value</input> 
} 
1

請通過你的模型視圖。

改變這一點:

return View(); 

return View(dat); 

希望這個作品

1

可以按如下方式實現它:

  1. 傳遞模型查看

    public ActionResult Index() 
    { 
        var model = //your model 
        return View(models); 
    } 
    
  2. 創建Strongly type View

    @model YourModelTypeName 
    
    @using (Html.BeginForm("TestHtmlRedirect", "Home", FormMethod.Post, null)) 
    { 
        // Your Controls 
        // for Eg: 
        // @Html.textboxfor(m => Model.Setting); // will create text box for setting property 
    <input type="submit" value="submit" /> 
    } 
    
  3. 捕獲在行動後的模型如下:

    [HttpPost] 
    public ActionResult Index(ModelType model) 
    { 
        // on submitting your text box values entered by ysers will get bind in model 
        // Your model will contain all values entered by user 
    }