2

I'm currently building an Html form that takes user input, creates a message body out of it, and sends it to me in an email. Since there are 10 different input fields the user needs to fill out, I'd like to concatenate the strings into a single string - messageBody. When I define the strings with { get; set; } in the model, they need to be Public so that I can access them on the form in the view. However, when I try to concatenate all of the strings in the model, I can't because "A field initializer cannot reference the non-static field." I tried changing the strings to "public static string" and it let me reference the strings in the model, but it could no longer reference the strings in the view because "cannot be accessed with an instance reference; qualify it with a type name instead." I switched them back to "public string" and thought maybe I could just concatenate them in Razor in the view itself, but when I try that, it doesn't recognize the strings because "The name "myString" doesn't exist in the current context."

I'm kind of lost on what to try next. From my understanding (which isn't that great), my strings MUST be public in order to access them from the view, but they also MUST be static for me to concatenate them into a single string? Doesn't make that much sense to me, I really hope I'm just screwing something up somewhere.

My model looks like:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace CuttingEdgeWebsite2._0.Models
{
    public class ServiceRequest
    {
        [Key]
        public int serviceRequestID { get; set; }
        [Required]
        public string serviceRequestName { get; set; }
        [Required]
        public string serviceRequestEmail { get; set; }
        public string serviceRequestCompany { get; set; }
        public string serviceRequestAddress { get; set; }
        [Required]
        public string serviceType { get; set; }
        public string serviceRequestCity { get; set; }
        public string serviceRequestState { get; set; }
        public string serviceRequestZip { get; set; }
        [Required]
        public string serviceRequestNumber { get; set; }
        public string serviceRequestComments { get; set; }
    }
}

My View looks like:

@model MyWebsite.Models.ServiceRequest

@using (Html.BeginForm("Email", "ServiceRequestsController", FormMethod.Post))
{
    <div class="servicesReq">
        <div class="serviceReqForm">
            <div class="col-left">
                <label>
                    <span class="required">* </span><span>Name:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestName)
                </label><br />
                        <label>
                            <span class="required">* </span><span>Email:</span><br />
                            @Html.TextBoxFor(model => model.serviceRequestEmail)
                        </label><br />
                <label>
                    <span>Company:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestCompany)
                </label><br />
                <label>
                    <span>Address:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestAddress)
                </label><br /><br />
            </div>
            <div class="serviceTypeGrid">
                <span class="required">* </span><span>Please choose what type of servicing you require:</span><br /><br />
            </div>
            <div class="col-left2">
                <label class="serviceType">
                    <input type="radio" name="serviceType" value="Electrical" /> Electrical<br />
                    <input type="radio" name="serviceType" value="Lighting" /> Lighting<br />
                    <input type="radio" name="serviceType" value="Energy" /> Energy<br />
                    <input type="radio" name="serviceType" value="Multiple" /> Multiple<br />
                </label><br /><br />
            </div>
            <div class="col-right">
                <label>
                    <span>City:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestCity)
                </label><br />
                <label>
                    <span>State:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestState)
                </label><br />
                <label>
                    <span>Zip Code:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestZip)
                </label><br />
                <label>
                    <span class="required">* </span><span>Phone Number:</span><br />
                    @Html.TextBoxFor(model => model.serviceRequestNumber)
                </label><br />
            </div>
            <label class="commentText">
                <span>Comments:</span><br />
                @Html.TextAreaFor(model => model.serviceRequestComments, new { @class = "servReqComment" })
            </label>
            <div class="formSubmitButton">
                <input type="submit" value="Submit" /><br />
                <br />
            </div>
        </div>

    </div>
}

Any ideas on where/how to concatenate strings in this situation would be greatly appreciated.

1
  • Please show the controllers post handling and where you are currently composing the email body. Commented Dec 20, 2018 at 15:41

2 Answers 2

4

I would not manage the data in Razor but instead let the controller manage and prepare it.

Your ServiceRequestController/Email method will receive an instance of ServiceRequest object as argument (I'm assuming this because you're using POST method).

On that method you can easily add a process to go through the properties of the ServiceRequest object and concatenate them as string (this is assuming you separate values by ',').

[HttpPost]
public ActionResult Email(ServiceRequest myData)
{
    try
    {
        var myConcatenatedSrr = 
        string.Join(",",myData.prop1,myData.prop2,myData.prop3, ....)
        //Your e-mail logic here... 
    }
    catch (System.Exception)
    {
        throw;
    }
}

Hope this helps.

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

Comments

1

I am getting that you are using different namespace on your view. Your model remains under CuttingEdgeWebsite2._0.Models and you are using MyWebsite.Models.ServiceRequest for your view.

To concatenate all the string you can follow several ways. Add GetMessage() method inside the model:

public string GetMessage() 
{
        return $"{serviceRequestID} {serviceRequestName} {serviceRequestEmail} {serviceRequestCompany} {serviceRequestAddress} {serviceType} {serviceRequestCity} {serviceRequestState} {serviceRequestZip} {serviceRequestNumber} {serviceRequestComments}"
}

And use this method anywhere you want. You also can follow the way described by @valvestater65

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.