'ASP.NET MVC getting id of selected item in list
I'm creating my first app using asp.net MVC, having a very hard time. I simply want to get a single selected item in my list, press my submit button and do something with the selected item. I feel like I have all the elements here. But when I press submit I'm getting a null error in my View file (cshtml). Been all over the web and can't see how to apply what I'm finding to what I have. Any help or direction would be appreciated.
Model
public List<string> GetFileName { get; set; }
public List<string> ID { get; set; }
public List<SelectListItem> FileSelect { get; set; }
public async Task ProcessRepo()
{
string project = "https:...my url";
string projectToken = "***";
#region Get List of file names
// Get list of files***********
string uri = $"{project}tree?private_token={projectToken}&ref=master";
string results = await client.GetStringAsync(uri);
FileList files = new FileList();
FileList id = new FileList();
GetFileName = files.GetList(results);
ID = id.GetID(results);
#endregion
}
Controller
public async Task<ActionResult> RepoFiles()
{
AssetBrowserModel _repoInfo = new AssetBrowserModel();
await _repoInfo.ProcessRepo();
var repoFiles = _repoInfo.GetFileName;
//Populate list box
var model = new AssetBrowserModel();
model.FileSelect = new List<SelectListItem>();
for (int i=0; i < _repoInfo.GetFileName.Count; i++)
{
model.FileSelect.Add(new SelectListItem()
{
Text = _repoInfo.GetFileName[i],
Value = _repoInfo.ID[i].ToString(),
Selected = false
});
}
return View(model);
View cshtml
<div class="form-horizontal">
@Html.ListBoxFor(m => m.ID, new SelectList(Model.GetFileName, "Value", "Text"),
new { @style = "width:300px;height:100px;"})
</div>
<div>
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.ID, new { id = "UserId" })<br />
<input type="submit" name="submit" class="submit" value="Download" />
}
</div>
FileList
class FileList
{
public List<string> GetList(string results)
{
string stringToCheck = "name";
List<string> fileNames = new List<string>();
string[] data = results.Split(new[] { ',', '\n' });
for (int i = 0; i < data.Length; i++)
{
if (getTreeData[i].Contains(stringToCheck))
{
fileNames.Add(data[i]);
}
}
return fileNames;
}
public List<string> GetID(string results)
{
string idToCheck = "id";
List<string> fileID = new List<string>();
string[] data = results.Split(new[] { ',', '\n' });
for (int i = 0; i < data.Length; i++)
{
if (data[i].Contains(idToCheck))
{
string[] newID = data[i].Split(':');
fileID.Add(newID[1]);
}
}
foreach(var x in fileID)
{
System.Diagnostics.Debug.WriteLine($"File ID is: {x}");
}
return fileID;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
