0

I have an experience in work with ASP.NET forms, but new to MVC.

How can I get data from shared views on postback?

In ASP.NET Forms I can write something like this:

ASP.NET Forms:

Model code:

public class MyModelItem
{
    // Just TextBox is enough for editing this
    public string SimpleProperty { get; set; }

    // For this property separate NestedItemEditor.ascx is required
    public MyModelNestedItem ComplexProperty { get; set; }
}

public class MyModelNestedItem
{
    public string FirstProperty { get; set; }        
    public string SecondProperty { get; set; }
}

Behavior:

Control for editing MyModelNestedItem is separate ASCX control NestedItemEditor.ascx

This is just for example, MyModelNestedItem can be much more complex, I just want to give idea what I mean.

Now when I showing this item for editing, I'm showing one asp:TextBox and one NestedItemEditor.ascx. On page postback I'm gathering data from both and that's it.

Problem with MVC:

When I'm trying to implement this scenario with MVC, I'm using customized EditorFor (through using UIHint and creating shared view). So this shared view Views\Shared\EditorTemplates\MyModelNestedItem.cshtml can now display data that is already in MyModelNestedItem property but I have no idea how to make it return new entered data.

When parent controller recieves a post request, data seems to be in Request.Form, but which is civilized way to reach it? Sure, the best solution will be if data will fetch automatically into the MyModelItem.ComplexProperty.

2 Answers 2

2

The action which is called on post needs to be something like:

    [HttpPost]
    public ActionResult Index(MyViewModel mdl)

Then all the properties of the model which have input controls (or hidden inputs) on the form will have the data which was entered on the form (or passed to it or modified by javascript, in the case of hidden inputs).

This assumes that MyViewModel is the model referenced in your view.

Sign up to request clarification or add additional context in comments.

Comments

0

Writing an ActionResult method in the controller with the complex type simply worked for me:

 public class Topic
{
    public Topic()
    {

    }
    public DetailsClass Details
    {
        get;
        set;
    }

}

public class DetailsClass
{
    public string TopicDetails
    {
        get;
        set;
    }
}

The view:

@modelTopic
@using (Html.BeginForm("Submit","Default"))
{
  @Html.EditorFor(m=>m.Details)
  @:<input type="submit" />
 }

The controller:

  public ActionResult Index()
    {

        Topic topic = new Topic();
        return View( topic); 
    }


    public ActionResult Submit(Topic t)
    {
        return View(t);
    }

When submited, the Topic t contains the value i ented within the editor (Assuming You have a custom editor for the complex type, DetailsClass in my sample)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.