1

I want to return all the object data that the EditorForModel receives from the user. The Event object has about 5 fields. I'd like to return the entire object to the Save method in the Controller where I can do a DB update. I can't use a foreach loop since the iEnumerator isn't available. How do I access the data within the Actionlink? Thanks!

@model EventsMVC.Models.Event

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
</head>
<body>
<div> 
        @Html.EditorForModel()
        @Html.ActionLink("Save", "Edit")
        @Html.ActionLink("Cancel and return to List", "Index")
   </div>
</body>
</html>
user1327418
  • 167
  • 1
  • 2
  • 14
  • You can use Model or ViewData or ViewBag – ArDumez May 26 '17 at 20:25
  • You do not use an action link. You use a form in your view and you submit you data to a POST method - `[HttpPost] public ActionResult Save(Event model)` –  May 26 '17 at 21:04
  • Like this? The object coming to the Save method is still null. The contoller is called Database. @using (Html.BeginForm("Save", "Database")) – user1327418 May 26 '17 at 21:16
  • 1
    @user1327418, That will work just fine (assuming its `@using (Html.BeginForm("Save", "Database")) { @Html.EditorForModel() }`. And if its not working for you, show all the relevant code including your model. –  May 26 '17 at 21:19
  • That worked! The input type submit button did it. The action link did not return the object data. Thanks! – user1327418 May 26 '17 at 21:22

2 Answers2

1

following code will be helpful for your problem:

Your changed View Code:

@model EventsMVC.Models.Event

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
</head>
<body>
<div> 
      //save - ActionName and Account(for ex)-ControllerName
       @using (Html.BeginForm("Save", "Account")) 

      { 

        @Html.EditorForModel() 
       <input type="submit" value="SubmitData" /> 

     }
   </div>
</body>
</html>

And in your Controller:

    [HttpPost]
    public ActionResult Save(Event eventmodel)
    {
         //your update logic here

    }

Hope Above information was helpful ,kindly let me know your thoughts or feedbacks

Thanks

Karthik

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12
  • shouldn't you add `[HttpPost]` attribute in your controller method? – kimbaudi May 27 '17 at 05:12
  • @kimbaudi Thank you so much for your additional note. As you said, even though [Httpget] and [Httppost] is optional attribute, it is alaways good to have in all the action methods. Useful link:https://stackoverflow.com/questions/5332275/httppost-vs-httpget-attributes-in-mvc-why-use-httppost Once again thank you and I will update the answer..:) – Karthik Elumalai May 27 '17 at 06:50
  • If you don't add `[HttpGet]` or `[HttpPost]`, then it defaults to `[HttpGet]` so I always recommend adding `[HttpPost]`. `[HttpGet]` is optional, but I like to be explicit and add it anyways :) – kimbaudi May 27 '17 at 07:03
0

I'm not certain I understand your question.

Usually in the controller method you include your model object as a parameter and the MVC framework will automatically fill the model object with data from your page.

something like

    public ActionResult Index(EventsMVC.Models.Event model)
    {
       //... do stuff with model
    }

these can then be referenced by the name properties in your model class directly.

Enumeration should not be required.

wode
  • 236
  • 3
  • 8
  • But how do I send that model back to the Controller method. When I add that parameter, it is null upon return. I need to get the stuff from the EditorForModel to the method. – user1327418 May 26 '17 at 20:42
  • the EditorForModel should automatically create javascript variable names that will be recognized my mvc when you click the action link and resubmit the page to MVC. – wode May 26 '17 at 20:50
  • perhaps there is an access or a naming issue with your model object / properties. – wode May 26 '17 at 20:51
  • Please post your model object and the controller method where you are trying to receive your model object. – wode May 26 '17 at 20:52
  • hmm... I apologize for missing this... but also when you use your editorFor and other similar statements in your view you specify the model property. something like.. @Html.EditorForModel(model => Model.PropertyName) – wode May 26 '17 at 20:55
  • The compiler doesn't recognize that syntax at all. Does the model binding need to be different for it to work that way? – user1327418 May 26 '17 at 21:04
  • I think Stephen Muecke made a key point.. it may not be posting the information needed to create the model when following an action Link. – wode May 26 '17 at 21:12