0

I'm trying to submit form data as an XML string through an API instead of the usual <form action="http://www.mysite.com" method="post">.

The API takes in an XML string as a parameter, like: <Data><firstnamex>Hello</firstnamex><lastnamex>World</lastnamex></Data>.

The old way in Web Forms I would do this was

String dataXml = "<Data>";
dataXml += "<firstnamex>" + firstnamex.Text + "</firstnamex>";
dataXml += "<lastnamex>" + lastnamex.Text + "</lastnamex>";
dataXml += "</Data>";

and then

mainApi.Service1 ws = new mainApi.Service1();
string retVal = ws.InsertRecord(dataXml);

Since I can't just grab the firstnamex.Text control like I could in Web Forms, how would I do this?

2
  • 1
    Get first name from your Model. A great place to start is asp.net/mvc. Commented Mar 19, 2013 at 14:50
  • I've done a bunch of MVC tutorials over the past couple months but I still can't figure it out. I know I should set up the model first and then create a view based on the model, but my issue is getting the result in an XML string after the user clicks submit. Commented Mar 19, 2013 at 16:44

2 Answers 2

1

You need a ViewModel that represents what you are collecting from the user on this "page" in order to complete the call. So first, create a ViewModel to hold that stuff. This ViewModel should also hold the code that actually calls your DAL. Its a bad idea to have your controller be too aware of what needs to be done to the actual data. Just let it know which method to call on the ViewModel, and also how to handle the returned value (if any).

public class YourViewModel 
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    // other fields here as needed.

    public YourViewModel() 
    {
        // you can put any field defaults you need here
    }

    public string InserRecord() 
    {
        String dataXml = "<Data>";
        dataXml += "<firstnamex>" + firstnamex.Text + "</firstnamex>";
        dataXml += "<lastnamex>" + lastnamex.Text + "</lastnamex>";
        dataXml += "</Data>";
        mainApi.Service1 ws = new mainApi.Service1();
        return ws.InsertRecord(dataXml);
    }
}

public class YourController 
{
    public ActionResult YourAction() 
    {
        var viewModel = new YourViewModel();
        return View(viewModel);
    }

    [HttpPost]
    public ActionResult YourAction(YourViewModel viewModel) 
    {
        var resultFromInsert = viewModel.InserRecord();
        // redirect here based on string returned above, or whatever.
    }
}

I like my Models Fat; my controllers Skinny; and my Views downright retarded.

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

2 Comments

No ... the ViewModel should be dumb. It should only be responsible for the information needed by the View. See lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models
I think those guys are really, really smart, but I just don't personally care for stuff like "Rule #2 – For each ViewModel type, there is defined exactly one strongly typed View". I make my ViewModels a little broader, and allow different Views to use them as needed. Its OK to thin-down the ViewModel, but I just don't think its absolutely required. I'm cool with my ViewModel knowing about my business domain directly, but if you want to add in another layer of abstraction for severability, that's fine too.
0

It would look something like this ...

[HttpPost]
public ActionResult YourAction(YourModel model)
{
    String dataXml = "<Data>";
    dataXml += "<firstnamex>" + model.FirstName + "</firstnamex>";
    dataXml += "<lastnamex>" + model.LastName + "</lastnamex>";
    dataXml += "</Data>";

    // ...

    return View(model); // or redirect or whatever
}

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.