gs11111
gs11111

Reputation: 659

Assign textbox using model with variable values

I have 4 textbox in the view and I need to assign values i get from the database in a variable all together because of my table struture. Now i'm facing problem to assign values to the controls in view and update the same database using the same controls.Please help,

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 i get all the values from tablestructure.I couldnt figureouthow to apply these values to textboxes i have in the view using model. as i need to update the same table using the textbox

table structure: ID(int), Settings(nvarchar) ,Desc (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 for replying. I get all values assigned except for tooltipcheckbox and kendo timepicker:

HEre is my code in controller:

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

//i get the value 0 in Tooltips //it throws error as string was not recognized as a valid boolean

settingsmodel:

 public bool tooltip { get; set; }

Upvotes: 0

Views: 1855

Answers (3)

mehmet mecek
mehmet mecek

Reputation: 2685

You should pass your model into the view.

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

And inside the view you can get the data from model.

Your model seems like a dictionary do your view side would be similar to the followings..

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

Upvotes: 1

Jatin patil
Jatin patil

Reputation: 4288

You can achieve it as follows:

  1. Pass your model to view

    public ActionResult Index()
    {
        var model = //your model
        return View(models); 
    } 
    
  2. Create 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. Capture the model in post action as follows:

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

Upvotes: 1

C M
C M

Reputation: 703

please pass your model to the view.

change this:

return View();

to

return View(dat);

hope this works

Upvotes: 1

Related Questions