Gyuzal
Gyuzal

Reputation: 1591

How to pass id attribute to controller's action in ASP.NET MVC project?

I'm new to asp.net and would veery appreciate if someone could help me.. I have a form in my view:

@using (Html.BeginForm("Edit", "Projects", FormMethod.Post, new { enctype = "multipart/form-data", id = serviceId })) 
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Project</legend>

            @Html.LabelFor(model => model.Title)     
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)       

            @Html.LabelFor(model => model.Description)

            @Html.TextAreaFor(model => model.Description, new { rows = 20})
            @Html.ValidationMessageFor(model => model.Description)

            <input type="submit" value="Save"  />

</fieldset>
}

serviceId i pass through ViewData (which is declared in the controller):

@{
    var serviceId = ViewData["serviceid"];
 }

Now on submitting i want to pass this serviceId to my ActionResult and inside this view i added script:

  $(function () {
    $("form").submit(function () {

        $.post("Projects/Edit", {Sid: $(this).attr('id'), }, function (data) {

        });
    });
});

and in my Projects controller, inside Edit ActionResult i get this id as a parameter:

[HttpPost]
    public ActionResult Edit(Models.Project EditedProject, int Sid)
    {
        Models.DBProjects dbproject = new Models.DBProjects();
        PoleInvestProjectContext context = new PoleInvestProjectContext();
        UserAccount curUser = context.Users.Where(u => u.UserName ==    User.Identity.Name).First();
        EditedProject.UserId = curUser.UserId;

        EditedProject.ServiceId = Sid;
        dbproject.EditProject(EditedProject);

        return RedirectToAction("Index", new { id = Sid });
    }

Here is an Error:( When i try to submit the form:

The parameters dictionary contains a null entry for parameter 'Sid' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(MyProject.Models.Project, Int32)' in 'MyProject.Controllers.ProjectsController'. 
An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

I would be soo grateful if someone could help! Thanks in advance!

Upvotes: 0

Views: 5568

Answers (1)

Forty-Two
Forty-Two

Reputation: 7605

Use a view model, ditch the javascript and the ViewData

public class EditViewModel()
{
  public string Title {get; set;}
  public string Description {get; set;}
  public int ServiceId {get; set;}
}

Set the ServiceId to the viewModel property instead of the ViewData object in the controller.

Strongly type your view to the viewModel instead of the entity object.

Then use a hidden field to pass the value back to the controller

@Html.LabelFor(model => model.Title)     
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)       
@Html.LabelFor(model => model.Description)
@Html.TextAreaFor(model => model.Description, new { rows = 20})
@Html.ValidationMessageFor(model => model.Description)

@Html.HiddenFor(model => model.ServiceId)

<input type="submit" value="Save"  />

And change the signature of you post action

[HttpPost]
public ActionResult Edit(EditViewModel viewModel)
{
  //...

Upvotes: 1

Related Questions