MVC3, Razor, Html.DropDownListFor and Select lists
I really hope someone can help me as I am getting COMPLETELY confused about what to do. Here's what I have right now
Controller:
ViewBag.TrialTopic = _trialTopicTable.GetAll(u => u.PartitionKey == PartitionKey);
return View(_dataTable.Get(u => u.PartitionKey == PartitionKey & u.RowKey == RowKey););
}
Model for the trial table
[DisplayName("Partition Key")]
public override string PartitionKey { get; set; }
[DisplayName("Row Key")]
public override string RowKey { get; set; }
....
public string TopicDescription { get; set; }
...
Model for the topic table
[DisplayName("Partition Key")]
public override string PartitionKey { get;开发者_开发百科 set; }
[DisplayName("Row Key")]
public override string RowKey { get; set; }
public string TopicDescription { get; set; }
View:
@Html.DropDownListFor(model => model.Trial.TopicDescription,
new SelectList(ViewBag.TrialTopic, "TrialDescription", "TrialDescription"))
Some explanation. The data returned in the ViewBag.TrialTopic is seven trial topics, the column name with the data in that table is "Trial Description". In the main table called Trial I also have the "column name" "Trial Description". When the view shows I would like it to show the current value of trial description in the drop down. When I select a new value from the drop down I'd like that to be saved away when I save the form.
I'm using Windows Azure table storage so I don't use any keys to store data in the main table. I store the full description.
The problem is that with the above code there seems to be no connection between the value in the drop down and the form. I've looked at so many different examples and I'm now totally confused. Hope someone can give me some advice.
thanks,
Mandy
Have you tried setting the selected value of the SelectList?
@Html.DropDownListFor(model => model.Trial.TopicDescription,
new SelectList(ViewBag.TrialTopic, "TrialDescription", "TrialDescription", Model.Trial.TopicDescription))
I might be wrong on what the Selected value should be. If so change that to the value it should be.
Should not it be:
Html.DropDownListFor(model => model.Trial.TopicDescription, new SelectList(ViewBag.TrialTopic, "TopicDescription", "TopicDescription"))
Notice the "value" and "text" field changes of the drop down.
精彩评论