简体   繁体   中英

MVC 4 - Pass Data from View => Controller

The entity "Bond" has a property named "Kauf" that is another entity ("Price" = date, addedBy, value).

Now, when in Create View of Bond ( = Buy), a value for Price needs to be entered. The standard Create View has no field for entering price data.

If I add

    <div class="editor-field">
        @Html.EditorFor(model => model.Kauf.Value)
        @Html.ValidationMessageFor(model => model.Kauf.Value)
    </div>

to the view, then how would I be able to grasp that value in the controller, where regularly only the entity "Bond" is accepted as parameter?

    [HttpPost]
    public ActionResult Create(Bond position)

Trying to access it through

    position.Kauf.Value 

would just reference the (yet) empty property "Kauf" from bond, I guess. Thank you for input!

Posting as an answer as this is too long to comment. I have tried to recreate and it is all working here so I thought I would post what I have so you can compare with what you are doing:

I am assuming that Kauf.Value is a string here...

Controller

[HttpGet]
public ActionResult Create()
{
    // Setup model before passing in
    var model = new Bond();
    return View(model);
}

[HttpPost]
public ActionResult Create(Bond position)
{
    string theValue = position.Kauf.Value;
    // At this point "theValue" contains a valid item
    return View(position);
}

View

@model MvcExperiments.Models.Bond

@using(@Html.BeginForm())
{
    <div class="editor-field">
        @Html.EditorFor(model => model.Kauf.Value)
        @Html.ValidationMessageFor(model => model.Kauf.Value)
    </div>

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

If by "entity" you are referring to an actual ORM entity then that's probably what the issue is - you should be using view models for passing data to/from your views, not raw entities. For example, you can try the following:

Model

public class KaufViewModel
{
    public double Price { get; set; }
    public string Value { get; set; }
    ...
}

public class BondViewModel
{
    public KaufViewModel Kauf { get; set; }
}

Controller

[HttpGet]
public ActionResult Create()
{
    return View(new BondViewModel());
}

[HttpPost]
public ActionResult Create(BondViewModel bond)
{
    // bond.Kauf.Value should be set at this point (given it's set in the form)
    return View(bond); // fields should be re-populated
}

View

@model BondViewModel

@using(@Html.BeginForm())
{
    @Html.EditorFor(model => model.Kauf)
    <p><input type="submit" value="Save" /></p>
}

None of the given answers here worked for me and I searched so much for a simple thing of passing data between action methods. So, here is my answer.

I have an action POST method that's called by a JS on a NavBar item click. This method instantiates a report instance, and passes it to be used by another action method. But here I'm returning a string clickedReport.

The ViewBag didn't work for me, returning the instance as a Model didn't work, and the only way it worked was by either using TempData or Session as shown below.

[HttpPost]
public ActionResult PopulateReport(int groupId, int itemId)
{
string clickedReport;

...

TempData["clickedReport"] = clickedReport;
Session["clickedReport"] = clickedReport;

return PartialView();

}

public ActionResult GridViewPartial()
{

// Now this other action method can get the clicked report assigned by the above action.
var tmpData = TempData["clickedReport"];
var sessionData = Session["clickedReport"];

// Runs the report instance and returns an object that holds a DataTable and other info to the grid that's in the calling partial view.

// Instantiate and populate GridConfig
GridConfig gridConfig = new GridConfig();
...

return PartialView("_GridViewPartial", gridConfig);

}

---- This is the view for the PopulateReport action ie PopulateReport.cshtml @Html.Action("GridViewPartial")


Hope this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM