Click here to Skip to main content
15,564,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo Everyone ,
My ViewModel(assetViewModel) always returns null and don't know why.I already tried a lot of Solution but nothing worked ,like [FromForm],[FromBody]...

What I have tried:

public class AssetViewModel
   {
       public int ID { get; set; }

       public string AssetName { get; set; }

       public IEnumerable<Department> Departments = Enum.GetValues(typeof(Department)).OfType<Department>();

       public IList<Country> Countries { get; set; }
       public string CountryName { get; set; }
       public string EMailAdress { get; set; }

       public DateTime PurchaseDate { get; set; }

       public bool broken = false;
   }


public class AssetController : Controller
{
public ActionResult Create()
{
var task = countryRepository.GetAllCountries(new Uri(countriesApiUrl + "regionalbloc/EU"));
var model = new AssetViewModel
{
Countries = task.Result
};
return View(model);

}

// POST: AssetController/Create
[HttpPost]
public ActionResult Create(AssetViewModel assetViewModel)
{
//all assetViewModel attributes are null


}

}



@model Domain.Models.ViewModels.AssetViewModel

@{
    ViewData["Title"] = "Create";
    
}

        <h1>Create</h1>

        <h4>AssetViewModel</h4>
        <hr />
        <div class="row">
            <div class="col-md-4">
                <form asp-action="Create" method="post" >
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>

                    <div class="form-group">
                        <label asp-for="AssetName" class="control-label"></label>
                        <input asp-for="AssetName" class="form-control" />
                        <span asp-validation-for="AssetName" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="Department" class="control-label"></label>
                        <select asp-for="Department">
                            @foreach (var item in Model.Departments)
                            {
                                <option value="@item">@item</option>
                            }
                        </select>
                        <span asp-validation-for="Department" class="text-danger"></span>
                        
                    </div>
                    <div class="form-group">
                        <label asp-for="CountryName" class="control-label"></label>
                        <select asp-for="CountryName" class="form-control">
                            @foreach (var item in Model.Countries)
                            {
                                <option value="@item.Name">@item.Name</option>
                            }
                        </select>
                        <span asp-validation-for="CountryName" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="EMailAdress" class="control-label"></label>
                        <input asp-for="EMailAdress" class="form-control" />
                        <span asp-validation-for="EMailAdress" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <label asp-for="PurchaseDate" class="control-label"></label>
                        <input asp-for="PurchaseDate" class="form-control" />
                        <span asp-validation-for="PurchaseDate" class="text-danger"></span>
                    </div>
                    <div class="form-group">
                        <input type="submit" value="Create" class="btn btn-primary" />
                    </div>
                </form>
            </div>
        </div>

        <div>
            <a asp-action="Index">Back to List</a>
        </div>

        @section Scripts {
            @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
        }
Posted
Updated 15-Jun-21 7:49am
v2
Comments
Richard Deeming 15-Jun-21 12:18pm    
Countries = task.Result

Not an answer to your question, but DO NOT do it like that! You risk deadlocking your application. At the very least, you'll significantly reduce the performance of your code, because you're blocking a request thread waiting for an external API call to complete.

ASP.NET has built-in support for asynchronous actions. Make your action method async, and await the task:
public async Task<ActionResult> Create()
{
    var countries = await countryRepository.GetAllCountries(new Uri(countriesApiUrl + "regionalbloc/EU"));
    var model = new AssetViewModel { Countries = countries };
    return View(model);
}
Member 12785541 15-Jun-21 12:21pm    
OK Richard, thanks for your response

1 solution

Hi ,

You have to assign value to viewmodel after you have to return model

var model = new AssetViewModel
{
Countries = task.Result
};
return View(model);


in your current model you are returning new model instead of you have to do following

var model = new AssetViewModel
{
model.CountryName = task.Result
model.EMailAdress ="Test@Test.com"
};
return View(model);

in above code you will get country name and email address as we have set the value of both

Thanks
Mehul Savani
 
Share this answer
 
Comments
Richard Deeming 15-Jun-21 12:28pm    
Your code does not differ significantly from the OP's code. You've simply set a default value for one other property, which isn't what they want, and introduced a syntax error, which is definitely not what they want.

It's the [HttpPost] method which is not working.
Member 12785541 15-Jun-21 12:35pm    
Thanks Savani ,but as Richard said , i have a Problem with the the [HttpPost] method not the the other one

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900