Redone
Redone

Reputation: 1303

Posted data contains only null values in asp.net mvc 4

the problem is whenever i try to save the data using the view. I always get null data. Please help. My codes for controller, models and view are as follows. Thanks.

public class OverallData
{
    public List<UserInfo> userInfo = new List<UserInfo>();
    public UserActivity userActivity = new UserActivity ();
}

public class UserInfo
{
    public int UserID { get; set; }
    public action UserName { get; set; }
    public action UserPassword { get; set; }

}

public class UserActivity
{
     public int ActivityID{ get; set; }
     public string ActionPerformed { get; set; }
}

View

@model BusinessProcess.Models.OverallData
<h1>List of Usernames</h1>
@foreach (var item in Model.userInfo) 
{
    @Html.DisplayFor(modelItem => item.UserName)
}


<h1>Enter some action Performed</h1>
@using (Html.BeginForm("EditUserActivity", "ManageUser", FormMethod.Post, new { id = "EditUserActivity" }))
{
@Html.ValidationSummary()

    @Html.DisplayNameFor(model => model.userActivity.ActionPerformed)
    @Html.EditorFor(model => model.userActivity.ActionPerformed)

    <button type="submit">Save</button>
}

Controller

[HttpPost]
public string EditUserActivity(UserActivity tempUserActivity)
{
    string postedValue = tempUserActivity.ActionPerformed;
    //When i debug this the "postedValue" is null
}

Upvotes: 2

Views: 1605

Answers (4)

CorrugatedAir
CorrugatedAir

Reputation: 819

The model binder is set to be using OverallData.

This is because of name of the input elements in your view.The model binder uses the names of the form elements to bind them to the class you're passing into the controller.

@Html.EditorFor(model => model.userActivity.ActionPerformed)

This will be generated with the name userActivity.ActionPerformed, so the model binding will look to assign it to userActivity.ActionPerformed, instead of just to ActionPerformed. Since OverallData doesn't have userActivity.ActionPerformed, that value doesn't get assigned to anything.

If you just pass in OverallData as your parameter, it'll work.

Edit:

Change your view model to look like this:

public class OverallData
{
    public OverallData()
    {
        userActivity = new UserActivity();
        userInfo = new List<UserInfo>();
    }
    public List<UserInfo> userInfo { get; set; }
    public UserActivity userActivity { get; set; }

}

Upvotes: 0

SpruceMoose
SpruceMoose

Reputation: 10320

I think your controller method EditUserActivity() should be expecting a parameter of type OverallData instead of UserActivity based on the model used in the view which may explain why ActionPerformed is null.

Upvotes: 1

Deepak Saralaya
Deepak Saralaya

Reputation: 457

just try

  [HttpPost]
  public string EditUserActivity(OverallData tempUserActivity)
   {
     string postedValue = tempUserActivity.userActivity.ActionPerformed;

   }

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

Your model is of type BusinessProcess.Models.OverallData so your action is expecting that type. I'd say you should change your action method to

[HttpPost]
public string EditUserActivity(OverallData overallData )
{
    string postedValue = overallData.userActivity.ActionPerformed;
}

Upvotes: 1

Related Questions